source: repository/lib/Metabrik/Remote/Wmi.pm

Last change on this file was 992:6bd6acfc81d5, checked in by GomoR <gomor@…>, 2 weeks ago
  • update: copyright notice
  • new: support for Kali Linux operating system
  • remove: api::onyphe is no more included, it use handled in its own repository
File size: 5.6 KB
Line 
1#
2# $Id$
3#
4# remote::wmi Brik
5#
6package Metabrik::Remote::Wmi;
7use strict;
8use warnings;
9
10use base qw(Metabrik::Shell::Command Metabrik::System::Package);
11
12sub brik_properties {
13   return {
14      revision => '$Revision$',
15      tags => [ qw(unstable) ],
16      author => 'GomoR <GomoR[at]metabrik.org>',
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#
57sub install {
58   my $self = shift;
59
60   # Install needed packages
61   $self->SUPER::install() or return;
62
63   my $datadir = $self->datadir;
64
65   my $version = '1.3.14';
66
67   my $url = 'http://www.openvas.org/download/wmi/wmi-'.$version.'.tar.bz2';
68   my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
69   my $files = $cw->mirror($url, "wmi-$version.tar.bz2", $datadir) or return;
70
71   if (@$files > 0) {
72      my $cmd = "tar jxvf $datadir/wmi-$version.tar.bz2 -C $datadir/";
73      $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   my $cwd = defined($self->shell) && $self->shell->pwd || '/tmp';
88   if (defined($self->shell)) {
89      $self->shell->run_cd("$datadir/wmi-$version/Samba/source") or return;
90   }
91   else {
92      chdir("$datadir/wmi-$version/Samba/source")
93         or return $self->log->error("install: chdir: $!");
94   }
95
96   $self->system('./autogen.sh') or return;
97   $self->system('./configure') or return;
98   $self->system('make "CPP=gcc -E -ffreestanding"') or return;
99   $self->system('make proto bin/wmic') or return;
100   $self->system('make proto bin/winexe') or return;
101
102   if (defined($self->shell)) {
103      $self->shell->run_cd($cwd);
104   }
105   else {
106      chdir($cwd) or return $self->log->error("install: chdir: $!");
107   }
108
109   my $sf = Metabrik::System::File->new_from_brik_init($self) or return;
110   $sf->sudo_copy("$datadir/wmi-$version/Samba/source/bin/wmic", '/usr/local/bin/') or return;
111   $sf->sudo_copy("$datadir/wmi-$version/Samba/source/bin/winexe", '/usr/local/bin/') or return;
112
113   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#
131sub request {
132   my $self = shift;
133   my ($query, $host, $user, $password) = @_;
134
135   $host ||= $self->host;
136   $user ||= $self->user;
137   $password ||= $self->password;
138   $self->brik_help_run_undef_arg('request', $query) or return;
139   $self->brik_help_run_undef_arg('request', $host) or return;
140   $self->brik_help_run_undef_arg('request', $user) or return;
141   $self->brik_help_run_undef_arg('request', $password) or return;
142
143   my $cmd = "wmic -U$user".'%'."$password //$host \"$query\"";
144
145   my $r = $self->SUPER::execute($cmd) or return;
146   #return $r;
147   if (@$r > 1) {
148      # First line is useless for us. Example: "CLASS: Win32_OperatingSystem"
149      shift @$r;
150      my $sp = Metabrik::String::Psv->new_from_brik_init($self) or return;
151      $sp->first_line_is_header(1);
152      # Need to desactivate double-quote parsing we may find in a process name
153      $sp->quote("'");
154      my $data = join("\n", @$r);
155      return $sp->decode($data);
156   }
157
158   return $r;
159}
160
161#
162# More requests:
163# http://wiki.monitoring-fr.org/nagios/windows-client/superivision-wmi
164#
165sub get_win32_operatingsystem {
166   my $self = shift;
167
168   return $self->request('SELECT * FROM Win32_OperatingSystem', @_);
169}
170
171sub get_win32_process {
172   my $self = shift;
173
174   return $self->request('SELECT * FROM Win32_Process', @_);
175}
176
1771;
178
179__END__
180
181=head1 NAME
182
183Metabrik::Remote::Wmi - remote::wmi Brik
184
185=head1 COPYRIGHT AND LICENSE
186
187Copyright (c) 2014-2019, Patrice E<lt>GomoRE<gt> Auffret
188
189You may distribute this module under the terms of The BSD 3-Clause License.
190See LICENSE file in the source distribution archive.
191
192=head1 AUTHOR
193
194Patrice E<lt>GomoRE<gt> Auffret
195
196=cut
Note: See TracBrowser for help on using the repository browser.