File Coverage

blib/lib/Metabrik/Remote/Winexe.pm
Criterion Covered Total %
statement 9 63 14.2
branch 0 54 0.0
condition 0 20 0.0
subroutine 3 7 42.8
pod 1 4 25.0
total 13 148 8.7


line stmt bran cond sub pod time code
1             #
2             # $Id$
3             #
4             # remote::winexe Brik
5             #
6             package Metabrik::Remote::Winexe;
7 1     1   640 use strict;
  1         3  
  1         28  
8 1     1   5 use warnings;
  1         2  
  1         29  
9              
10 1     1   5 use base qw(Metabrik::Shell::Command Metabrik::System::Package);
  1         2  
  1         1002  
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             },
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             #
71             sub install {
72 0     0 0   my $self = shift;
73              
74             # Install needed packages
75 0 0         $self->SUPER::install() or return;
76              
77 0           my $datadir = $self->datadir;
78              
79 0           my $version = '1.3.14';
80              
81 0           my $url = 'http://www.openvas.org/download/wmi/wmi-'.$version.'.tar.bz2';
82 0 0         my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
83 0 0         my $files = $cw->mirror($url, "wmi-$version.tar.bz2", $datadir) or return;
84              
85 0 0         if (@$files > 0) {
86 0           my $cmd = "tar jxvf $datadir/wmi-$version.tar.bz2 -C $datadir/";
87 0 0         $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 0   0       my $cwd = defined($self->shell) && $self->shell->pwd || '/tmp';
98 0 0         if (defined($self->shell)) {
99 0 0         $self->shell->run_cd("$datadir/wmi-$version/Samba/source") or return;
100             }
101             else {
102 0 0         chdir("$datadir/wmi-$version/Samba/source")
103             or return $self->log->error("install: chdir: $!");
104             }
105              
106 0 0         $self->system('./autogen.sh') or return;
107 0 0         $self->system('./configure') or return;
108 0 0         $self->system('make "CPP=gcc -E -ffreestanding"') or return;
109 0 0         $self->system('make proto bin/wmic') or return;
110 0 0         $self->system('make proto bin/winexe') or return;
111              
112 0 0         if (defined($self->shell)) {
113 0           $self->shell->run_cd($cwd);
114             }
115             else {
116 0 0         chdir($cwd) or return $self->log->error("install: chdir: $!");
117             }
118              
119 0 0         my $sf = Metabrik::System::File->new_from_brik_init($self) or return;
120 0 0         $sf->copy("$datadir/wmi-$version/Samba/source/bin/wmic", '/usr/local/bin/') or return;
121 0 0         $sf->copy("$datadir/wmi-$version/Samba/source/bin/winexe", '/usr/local/bin/') or return;
122              
123 0           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             #
146             sub execute {
147 0     0 0   my $self = shift;
148 0           my ($command, $host, $user, $password) = @_;
149              
150 0   0       $host ||= $self->host;
151 0   0       $user ||= $self->user;
152 0   0       $password ||= $self->password;
153 0 0         $self->brik_help_run_undef_arg('execute', $command) or return;
154 0 0         $self->brik_help_run_undef_arg('execute', $host) or return;
155 0 0         $self->brik_help_run_undef_arg('execute', $user) or return;
156 0 0         $self->brik_help_run_undef_arg('execute', $password) or return;
157              
158 0           my $winexe = 'winexe';
159              
160             # If winexe 1.1 is available, we use it instead of venerable winexe from wmi package.
161 0 0         if (-f '/usr/local/bin/winexe11') {
162 0           $self->log->verbose("execute: winexe11 found, using it");
163 0           $winexe = 'winexe11';
164             }
165              
166             # Do not put $command between quotes, let user do it.
167 0           my $cmd = "$winexe -U$user".'%'."$password //$host $command";
168              
169 0           $self->log->verbose("execute: cmd[$cmd]");
170              
171 0           return $self->SUPER::execute($cmd);
172             }
173              
174             sub execute_in_background {
175 0     0 0   my $self = shift;
176 0           my ($command, $host, $user, $password) = @_;
177              
178 0   0       $host ||= $self->host;
179 0   0       $user ||= $self->user;
180 0   0       $password ||= $self->password;
181 0 0         $self->brik_help_run_undef_arg('execute_in_background', $command) or return;
182 0 0         $self->brik_help_run_undef_arg('execute_in_background', $host) or return;
183 0 0         $self->brik_help_run_undef_arg('execute_in_background', $user) or return;
184 0 0         $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 0           my $cmd = "winexe -U$user".'%'."$password //$host $command &";
188              
189 0           return $self->SUPER::execute($cmd);
190             }
191              
192             1;
193              
194             __END__