source: repository/lib/Metabrik/Remote/Winexe.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: 6.3 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         kali => [  qw(build-essential autoconf) ],
42      },
43   };
44}
45
46#
47# Compilation process
48#
49# http://techedemic.com/2014/09/17/installing-wmic-in-ubuntu-14-04-lts-64-bit/
50# http://wiki.monitoring-fr.org/nagios/windows-client/superivision-wmi
51#
52# 2017-02-20: New compilation process for winexe 1.1:
53#
54# http://rand0m.org/2015/08/05/winexe-1-1-centos-6-and-windows-2012-r2/
55# https://sourceforge.net/p/winexe/winexe-waf/ci/master/tree/
56#
57# sudo apt-get install gcc-mingw-w64 comerr-dev libpopt-dev libbsd-dev zlib1g-dev
58#    libc6-dev python-dev gnutls-dev acl-dev libldap-dev
59# git clone git://git.code.sf.net/p/winexe/winexe-waf winexe-winexe-waf
60# wget https://download.samba.org/pub/samba/stable/samba-4.1.23.tar.gz
61# tar zxvf samba-4.1.23.tar.gz
62# cd winexe-winexe-waf/source
63# vi wscript_build
64# -        stlib='smb_static bsd z resolv rt',
65# -        lib='dl'
66# +        stlib='smb_static z rt',
67# +        lib='dl resolv bsd'
68# ./waf --samba-dir=../../samba-4.1.23 configure build
69# cp build/winexe-static /usr/local/bin/winexe11
70#
71sub install {
72   my $self = shift;
73
74   # Install needed packages
75   $self->SUPER::install() or return;
76
77   my $datadir = $self->datadir;
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 = defined($self->shell) && $self->shell->pwd || '/tmp';
98   if (defined($self->shell)) {
99      $self->shell->run_cd("$datadir/wmi-$version/Samba/source") or return;
100   }
101   else {
102      chdir("$datadir/wmi-$version/Samba/source")
103         or return $self->log->error("install: chdir: $!");
104   }
105
106   $self->system('./autogen.sh') or return;
107   $self->system('./configure') or return;
108   $self->system('make "CPP=gcc -E -ffreestanding"') or return;
109   $self->system('make proto bin/wmic') or return;
110   $self->system('make proto bin/winexe') or return;
111
112   if (defined($self->shell)) {
113      $self->shell->run_cd($cwd);
114   }
115   else {
116      chdir($cwd) or return $self->log->error("install: chdir: $!");
117   }
118
119   my $sf = Metabrik::System::File->new_from_brik_init($self) or return;
120   $sf->copy("$datadir/wmi-$version/Samba/source/bin/wmic", '/usr/local/bin/') or return;
121   $sf->copy("$datadir/wmi-$version/Samba/source/bin/winexe", '/usr/local/bin/') or return;
122
123   return 1;
124}
125
126#
127# A. Activate file sharing on local network
128#
129# B. Instructions to activate WINEXESVC under Windows 7
130#
131# 1. Add LocalAccountTokenFilterPolicy registry key
132#
133#   runas administrator a cmd.exe
134#
135#   reg add "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\system"
136#      /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 1 /f
137#
138# 2. Add winexesvc service (update: not necessary since winexe 1.1, it installs the
139#    service by itself)
140#
141#   runas administrator a cmd.exe
142#
143# C:\> sc create winexesvc binPath= C:\WINDOWS\WINEXESVC.EXE start= auto DisplayName= winexesvc
144# C:\> sc description winexesvc "Remote command provider"
145#
146sub execute {
147   my $self = shift;
148   my ($command, $host, $user, $password) = @_;
149
150   $host ||= $self->host;
151   $user ||= $self->user;
152   $password ||= $self->password;
153   $self->brik_help_run_undef_arg('execute', $command) or return;
154   $self->brik_help_run_undef_arg('execute', $host) or return;
155   $self->brik_help_run_undef_arg('execute', $user) or return;
156   $self->brik_help_run_undef_arg('execute', $password) or return;
157
158   my $winexe = 'winexe';
159
160   # If winexe 1.1 is available, we use it instead of venerable winexe from wmi package.
161   if (-f '/usr/local/bin/winexe11') {
162      $self->log->verbose("execute: winexe11 found, using it");
163      $winexe = 'winexe11';
164   }
165
166   # Do not put $command between quotes, let user do it.
167   my $cmd = "$winexe -U$user".'%'."$password //$host $command";
168
169   $self->log->verbose("execute: cmd[$cmd]");
170
171   return $self->SUPER::execute($cmd);
172}
173
174sub execute_in_background {
175   my $self = shift;
176   my ($command, $host, $user, $password) = @_;
177
178   $host ||= $self->host;
179   $user ||= $self->user;
180   $password ||= $self->password;
181   $self->brik_help_run_undef_arg('execute_in_background', $command) or return;
182   $self->brik_help_run_undef_arg('execute_in_background', $host) or return;
183   $self->brik_help_run_undef_arg('execute_in_background', $user) or return;
184   $self->brik_help_run_undef_arg('execute_in_background', $password) or return;
185
186   # Do not put $command between quotes, let user do it.
187   my $cmd = "winexe -U$user".'%'."$password //$host $command &";
188
189   return $self->SUPER::execute($cmd);
190}
191
1921;
193
194__END__
195
196=head1 NAME
197
198Metabrik::Remote::Winexe - remote::winexe Brik
199
200=head1 COPYRIGHT AND LICENSE
201
202Copyright (c) 2014-2019, Patrice E<lt>GomoRE<gt> Auffret
203
204You may distribute this module under the terms of The BSD 3-Clause License.
205See LICENSE file in the source distribution archive.
206
207=head1 AUTHOR
208
209Patrice E<lt>GomoRE<gt> Auffret
210
211=cut
Note: See TracBrowser for help on using the repository browser.