File Coverage

blib/lib/Metabrik/Remote/Wmi.pm
Criterion Covered Total %
statement 9 59 15.2
branch 0 50 0.0
condition 0 11 0.0
subroutine 3 8 37.5
pod 1 5 20.0
total 13 133 9.7


line stmt bran cond sub pod time code
1             #
2             # $Id$
3             #
4             # remote::wmi Brik
5             #
6             package Metabrik::Remote::Wmi;
7 1     1   650 use strict;
  1         2  
  1         29  
8 1     1   6 use warnings;
  1         1  
  1         29  
9              
10 1     1   6 use base qw(Metabrik::Shell::Command Metabrik::System::Package);
  1         1  
  1         866  
11              
12             sub brik_properties {
13             return {
14 0     0 1   revision => '$Revision$',
15             tags => [ qw(unstable) ],
16             author => 'GomoR ',
17             license => 'http://opensource.org/licenses/BSD-3-Clause',
18             attributes => {
19             datadir => [ qw(datadir) ],
20             host => [ qw(host) ],
21             user => [ qw(username) ],
22             password => [ qw(password) ],
23             },
24             attributes_default => {
25             as_array => 1,
26             capture_mode => 1,
27             use_globbing => 0,
28             },
29             commands => {
30             install => [ ], # Inherited
31             request => [ qw(query host|OPTIONAL user|OPTIONAL password|OPTIONAL) ],
32             get_win32_operatingsystem => [ qw(host|OPTIONAL user|OPTIONAL password|OPTIONAL) ],
33             get_win32_process => [ qw(host|OPTIONAL user|OPTIONAL password|OPTIONAL) ],
34             },
35             require_modules => {
36             'Metabrik::Client::Www' => [ ],
37             'Metabrik::System::File' => [ ],
38             'Metabrik::String::Psv' => [ ],
39             },
40             require_binaries => {
41             tar => [ ],
42             wmic => [ ],
43             },
44             need_packages => {
45             ubuntu => [ qw(build-essential autoconf) ],
46             debian => [ qw(build-essential autoconf) ],
47             kali => [ qw(build-essential autoconf) ],
48             },
49             };
50             }
51              
52             #
53             # Compilation process
54             # http://techedemic.com/2014/09/17/installing-wmic-in-ubuntu-14-04-lts-64-bit/
55             # http://wiki.monitoring-fr.org/nagios/windows-client/superivision-wmi
56             #
57             sub install {
58 0     0 0   my $self = shift;
59              
60             # Install needed packages
61 0 0         $self->SUPER::install() or return;
62              
63 0           my $datadir = $self->datadir;
64              
65 0           my $version = '1.3.14';
66              
67 0           my $url = 'http://www.openvas.org/download/wmi/wmi-'.$version.'.tar.bz2';
68 0 0         my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
69 0 0         my $files = $cw->mirror($url, "wmi-$version.tar.bz2", $datadir) or return;
70              
71 0 0         if (@$files > 0) {
72 0           my $cmd = "tar jxvf $datadir/wmi-$version.tar.bz2 -C $datadir/";
73 0 0         $self->SUPER::execute($cmd) or return;
74             }
75              
76             # cd wmi-$version/Samba/source
77             # ./autogen.sh
78             # ./configure
79             # On Ubuntu 16.10, we have to comment line 583 from wmi-1.3.14/Samba/source/pidl/pidl
80             # Error message was:
81             # Can't use 'defined(@array)' (Maybe you should just omit the defined()?) at ./pidl/pidl line 583.
82             # Makefile:28886: recipe for target 'idl' failed
83             # make "CPP=gcc -E -ffreestanding"
84             # make proto bin/wmic
85             # make proto bin/winexe
86              
87 0   0       my $cwd = defined($self->shell) && $self->shell->pwd || '/tmp';
88 0 0         if (defined($self->shell)) {
89 0 0         $self->shell->run_cd("$datadir/wmi-$version/Samba/source") or return;
90             }
91             else {
92 0 0         chdir("$datadir/wmi-$version/Samba/source")
93             or return $self->log->error("install: chdir: $!");
94             }
95              
96 0 0         $self->system('./autogen.sh') or return;
97 0 0         $self->system('./configure') or return;
98 0 0         $self->system('make "CPP=gcc -E -ffreestanding"') or return;
99 0 0         $self->system('make proto bin/wmic') or return;
100 0 0         $self->system('make proto bin/winexe') or return;
101              
102 0 0         if (defined($self->shell)) {
103 0           $self->shell->run_cd($cwd);
104             }
105             else {
106 0 0         chdir($cwd) or return $self->log->error("install: chdir: $!");
107             }
108              
109 0 0         my $sf = Metabrik::System::File->new_from_brik_init($self) or return;
110 0 0         $sf->sudo_copy("$datadir/wmi-$version/Samba/source/bin/wmic", '/usr/local/bin/') or return;
111 0 0         $sf->sudo_copy("$datadir/wmi-$version/Samba/source/bin/winexe", '/usr/local/bin/') or return;
112              
113 0           return 1;
114             }
115              
116             #
117             # Must add specific user everywhere
118             #
119             # Howto enable WMI on a Windows machine
120             # http://community.zenoss.org/docs/DOC-4517
121             #
122             # Troubleshoot WMI connexion issues:
123             # wbemtest.exe + https://msdn.microsoft.com/en-us/library/windows/desktop/aa394603(v=vs.85).aspx
124             #
125             # dcomcnfg => DCOM permission for user
126             # Computer/Manage/Properties => 'WMI Control/Properties/Security'
127             #
128             # Open firewall for DCOM service
129             # http://www.returnbooleantrue.com/2014/10/enabling-wmi-on-windows-azure.html
130             #
131             sub request {
132 0     0 0   my $self = shift;
133 0           my ($query, $host, $user, $password) = @_;
134              
135 0   0       $host ||= $self->host;
136 0   0       $user ||= $self->user;
137 0   0       $password ||= $self->password;
138 0 0         $self->brik_help_run_undef_arg('request', $query) or return;
139 0 0         $self->brik_help_run_undef_arg('request', $host) or return;
140 0 0         $self->brik_help_run_undef_arg('request', $user) or return;
141 0 0         $self->brik_help_run_undef_arg('request', $password) or return;
142              
143 0           my $cmd = "wmic -U$user".'%'."$password //$host \"$query\"";
144              
145 0 0         my $r = $self->SUPER::execute($cmd) or return;
146             #return $r;
147 0 0         if (@$r > 1) {
148             # First line is useless for us. Example: "CLASS: Win32_OperatingSystem"
149 0           shift @$r;
150 0 0         my $sp = Metabrik::String::Psv->new_from_brik_init($self) or return;
151 0           $sp->first_line_is_header(1);
152             # Need to desactivate double-quote parsing we may find in a process name
153 0           $sp->quote("'");
154 0           my $data = join("\n", @$r);
155 0           return $sp->decode($data);
156             }
157              
158 0           return $r;
159             }
160              
161             #
162             # More requests:
163             # http://wiki.monitoring-fr.org/nagios/windows-client/superivision-wmi
164             #
165             sub get_win32_operatingsystem {
166 0     0 0   my $self = shift;
167              
168 0           return $self->request('SELECT * FROM Win32_OperatingSystem', @_);
169             }
170              
171             sub get_win32_process {
172 0     0 0   my $self = shift;
173              
174 0           return $self->request('SELECT * FROM Win32_Process', @_);
175             }
176              
177             1;
178              
179             __END__