Remote/Winexe.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Remote/Windows.pm ↑ Summary Remote/Winsvc.pm »
1 #
2 # $Id$
3 #
4 # remote::winexe Brik
5 #
6 package Metabrik::Remote::Winexe;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik::Shell::Command Metabrik::System::Package);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub 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 #
71 sub 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 #
146 sub 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  
174 sub 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  
192 1;
193  
194 __END__
« Remote/Windows.pm ↑ Summary Remote/Winsvc.pm »