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

Last change on this file was 887:73850be884c6, checked in by GomoR <gomor@…>, 2 months ago
  • bugfix: client::smbclient: get only filename part from a file to upload and correctly put it in remote_path for upload Command
  • update: remote::winexe: new instructions to compile winexe 1.1 and support for Windows 10
File size: 5.9 KB
Line 
1#
2# $Id$
3#
4# remote::winexe Brik
5#
6package Metabrik::Remote::Winexe;
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      },
26      commands => {
27         install => [ ], # Inherited
28         execute => [ qw(command host|OPTIONAL user|OPTIONAL password|OPTIONAL) ],
29         execute_in_background => [ qw(command host|OPTIONAL user|OPTIONAL password|OPTIONAL) ],
30      },
31      require_modules => {
32         'Metabrik::System::File' => [ ],
33      },
34      require_binaries => {
35         tar => [ ],
36         winexe => [ ],
37      },
38      need_packages => {
39         ubuntu => [  qw(build-essential autoconf) ],
40         debian => [  qw(build-essential autoconf) ],
41      },
42   };
43}
44
45#
46# Compilation process
47#
48# http://techedemic.com/2014/09/17/installing-wmic-in-ubuntu-14-04-lts-64-bit/
49# http://wiki.monitoring-fr.org/nagios/windows-client/superivision-wmi
50#
51# 2017-02-20: New compilation process for winexe 1.1:
52#
53# http://rand0m.org/2015/08/05/winexe-1-1-centos-6-and-windows-2012-r2/
54# https://sourceforge.net/p/winexe/winexe-waf/ci/master/tree/
55#
56# sudo apt-get install gcc-mingw-w64 comerr-dev libpopt-dev libbsd-dev zlib1g-dev
57#    libc6-dev python-dev gnutls-dev acl-dev libldap-dev
58# git clone git://git.code.sf.net/p/winexe/winexe-waf winexe-winexe-waf
59# wget https://download.samba.org/pub/samba/stable/samba-4.1.23.tar.gz
60# tar zxvf samba-4.1.23.tar.gz
61# cd winexe-winexe-waf/source
62# vi wscript_build
63# -        stlib='smb_static bsd z resolv rt',
64# -        lib='dl'
65# +        stlib='smb_static z rt',
66# +        lib='dl resolv bsd'
67# ./waf --samba-dir=../../samba-4.1.23 configure build
68# cp build/winexe-static /usr/local/bin/winexe11
69#
70sub install {
71   my $self = shift;
72
73   # Install needed packages
74   $self->SUPER::install() or return;
75
76   my $datadir = $self->datadir;
77   my $shell = $self->shell;
78
79   my $version = '1.3.14';
80
81   my $url = 'http://www.openvas.org/download/wmi/wmi-'.$version.'.tar.bz2';
82   my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
83   my $files = $cw->mirror($url, "wmi-$version.tar.bz2", $datadir) or return;
84
85   if (@$files > 0) {
86      my $cmd = "tar jxvf $datadir/wmi-$version.tar.bz2 -C $datadir/";
87      $self->SUPER::execute($cmd) or return;
88   }
89
90   # cd wmi-$version/Samba/source
91   # ./autogen.sh
92   # ./configure
93   # make "CPP=gcc -E -ffreestanding"
94   # make proto bin/wmic
95   # make proto bin/winexe
96
97   my $cwd = $shell->pwd;
98   $shell->run_cd("$datadir/wmi-$version/Samba/source") or return;
99
100   $self->system('./autogen.sh') or return;
101   $self->system('./configure') or return;
102   $self->system('make "CPP=gcc -E -ffreestanding"') or return;
103   $self->system('make proto bin/wmic') or return;
104   $self->system('make proto bin/winexe') or return;
105
106   $shell->run_cd($cwd);
107
108   my $sf = Metabrik::System::File->new_from_brik_init($self) or return;
109   $sf->copy("$datadir/wmi-$version/Samba/source/bin/wmic", '/usr/local/bin/') or return;
110   $sf->copy("$datadir/wmi-$version/Samba/source/bin/winexe", '/usr/local/bin/') or return;
111
112   return 1;
113}
114
115#
116# A. Activate file sharing on local network
117#
118# B. Instructions to activate WINEXESVC under Windows 7
119#
120# 1. Add LocalAccountTokenFilterPolicy registry key
121#
122#   runas administrator a cmd.exe
123#
124#   reg add "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\system"
125#      /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 1 /f
126#
127# 2. Add winexesvc service (update: not necessary since winexe 1.1, it installs the
128#    service by itself)
129#
130#   runas administrator a cmd.exe
131#
132# C:\> sc create winexesvc binPath= C:\WINDOWS\WINEXESVC.EXE start= auto DisplayName= winexesvc
133# C:\> sc description winexesvc "Remote command provider"
134#
135sub execute {
136   my $self = shift;
137   my ($command, $host, $user, $password) = @_;
138
139   $host ||= $self->host;
140   $user ||= $self->user;
141   $password ||= $self->password;
142   $self->brik_help_run_undef_arg('execute', $command) or return;
143   $self->brik_help_run_undef_arg('execute', $host) or return;
144   $self->brik_help_run_undef_arg('execute', $user) or return;
145   $self->brik_help_run_undef_arg('execute', $password) or return;
146
147   my $winexe = 'winexe';
148
149   # If winexe 1.1 is available, we use it instead of venerable winexe from wmi package.
150   if (-f '/usr/local/bin/winexe11') {
151      $self->log->verbose("execute: winexe11 found, using it");
152      $winexe = 'winexe11';
153   }
154
155   # Do not put $command between quotes, let user do it.
156   my $cmd = "$winexe -U$user".'%'."$password //$host $command";
157
158   $self->log->verbose("execute: cmd[$cmd]");
159
160   return $self->SUPER::execute($cmd);
161}
162
163sub execute_in_background {
164   my $self = shift;
165   my ($command, $host, $user, $password) = @_;
166
167   $host ||= $self->host;
168   $user ||= $self->user;
169   $password ||= $self->password;
170   $self->brik_help_run_undef_arg('execute_in_background', $command) or return;
171   $self->brik_help_run_undef_arg('execute_in_background', $host) or return;
172   $self->brik_help_run_undef_arg('execute_in_background', $user) or return;
173   $self->brik_help_run_undef_arg('execute_in_background', $password) or return;
174
175   # Do not put $command between quotes, let user do it.
176   my $cmd = "winexe -U$user".'%'."$password //$host $command &";
177
178   return $self->SUPER::execute($cmd);
179}
180
1811;
182
183__END__
184
185=head1 NAME
186
187Metabrik::Remote::Winexe - remote::winexe Brik
188
189=head1 COPYRIGHT AND LICENSE
190
191Copyright (c) 2014-2017, Patrice E<lt>GomoRE<gt> Auffret
192
193You may distribute this module under the terms of The BSD 3-Clause License.
194See LICENSE file in the source distribution archive.
195
196=head1 AUTHOR
197
198Patrice E<lt>GomoRE<gt> Auffret
199
200=cut
Note: See TracBrowser for help on using the repository browser.