| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # remote::sysmon Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Remote::Sysmon; | ||
| 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::Remote::Winexe Metabrik::Client::Smbclient); |
|
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) ], # Inherited | ||
| 21 | user => [ qw(username) ], # Inherited | ||
| 22 | password => [ qw(password) ], # Inherited | ||
| 23 | remote_path => [ qw(path) ], # Inherited | ||
| 24 | domain => [ qw(domain) ], # Inherited | ||
| 25 | sysmon_exe => [ qw(version) ], | ||
| 26 | conf_file => [ qw(file) ], | ||
| 27 | }, | ||
| 28 | attributes_default => { | ||
| 29 | sysmon_exe => 'Sysmon64.exe', | ||
| 30 | conf_file => 'sysmon.xml', | ||
| 31 | }, | ||
| 32 | commands => { | ||
| 33 | update => [ ], | ||
| 34 | get_sysmon_exe => [ ], | ||
| 35 | deploy => [ qw(host|OPTIONAL user|OPTIONAL password|OPTIONAL) ], | ||
| 36 | undeploy => [ ], | ||
| 37 | redeploy => [ ], | ||
| 38 | generate_conf => [ ], | ||
| 39 | update_conf => [ ], | ||
| 40 | }, | ||
| 41 | require_modules => { | ||
| 42 | 'Metabrik::Client::Www' => [ ], | ||
| 43 | 'Metabrik::File::Compress' => [ ], | ||
| 44 | 'Metabrik::File::Text' => [ ], | ||
| 45 | }, | ||
| 46 | require_binaries => { | ||
| 47 | }, | ||
| 48 | optional_binaries => { | ||
| 49 | }, | ||
| 50 | need_packages => { | ||
| 51 | }, | ||
| 52 | }; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub update { | ||
| 56 | my $self = shift; | ||
| 57 | |||
| 58 | my $datadir = $self->datadir; | ||
| 59 | |||
| 60 | my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return; | ||
| 61 | $cw->user_agent("Metabrik-Remote-Sysmon-mirror/1.00"); | ||
| 62 | $cw->datadir($datadir); | ||
| 63 | |||
| 64 | my $fc = Metabrik::File::Compress->new_from_brik_init($self) or return; | ||
| 65 | $fc->datadir($datadir); | ||
| 66 | |||
| 67 | my $input = 'Sysmon.zip'; | ||
| 68 | my $files = $cw->mirror('https://download.sysinternals.com/files/'.$input); | ||
| 69 | if (! defined($files)) { | ||
| 70 | return $self->log->errpr("update: mirror fail"); | ||
| 71 | } | ||
| 72 | |||
| 73 | my @updated = (); | ||
| 74 | for my $this (@$files) { | ||
| 75 | my $this_files = $fc->unzip($this, $datadir) or return; | ||
| 76 | push @updated, @$this_files; | ||
| 77 | } | ||
| 78 | |||
| 79 | return \@updated; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub get_sysmon_exe { | ||
| 83 | my $self = shift; | ||
| 84 | |||
| 85 | my $datadir = $self->datadir; | ||
| 86 | my $sysmon_exe = $self->sysmon_exe; | ||
| 87 | my $full_path = "$datadir/$sysmon_exe"; | ||
| 88 | |||
| 89 | if (-f $full_path) { | ||
| 90 | return $full_path; | ||
| 91 | } | ||
| 92 | |||
| 93 | return $self->log->error("get_sysmon_exe: file not found, call update Command"); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub generate_conf { | ||
| 97 | my $self = shift; | ||
| 98 | my ($conf_file) = @_; | ||
| 99 | |||
| 100 | $conf_file ||= $self->conf_file; | ||
| 101 | $self->brik_help_set_undef_arg('generate_conf', $conf_file) or return; | ||
| 102 | |||
| 103 | my $datadir = $self->datadir; | ||
| 104 | $conf_file = "$datadir/$conf_file"; | ||
| 105 | |||
| 106 | my $conf =<<EOF | ||
| 107 | <Sysmon schemaversion="3.30"> | ||
| 108 | <HashAlgorithms>SHA1</HashAlgorithms> | ||
| 109 | <!-- Log everything --> | ||
| 110 | <EventFiltering> | ||
| 111 | <ProcessCreate onmatch="exclude" /> <!-- event_id:1 --> | ||
| 112 | <FileCreateTime onmatch="exclude" /> <!-- event_id:2 --> | ||
| 113 | <NetworkConnect onmatch="exclude" /> <!-- event_id:3 --> | ||
| 114 | <!--SYSMON EVENT ID 4 : RESERVED FOR SYSMON STATUS MESSAGES, THIS LINE IS INCLUDED FOR DOCUMENTATION PURPOSES ONLY --> | ||
| 115 | <ProcessTerminate onmatch="exclude" /> <!-- event_id:5 --> | ||
| 116 | <DriverLoad onmatch="exclude" /> <!-- event_id:6 --> | ||
| 117 | <ImageLoad onmatch="exclude" /> <!-- event_id:7 --> | ||
| 118 | <CreateRemoteThread onmatch="exclude" /> <!-- event_id:8 --> | ||
| 119 | <RawAccessRead onmatch="exclude" /> <!-- event_id:9 --> | ||
| 120 | <ProcessAccess onmatch="exclude" /> <!-- event_id:10 --> | ||
| 121 | <FileCreate onmatch="exclude" /> <!-- event_id:11 --> | ||
| 122 | <RegistryEvent onmatch="exclude" /> <!-- event_id:12,13,14 --> | ||
| 123 | <FileCreateStreamHash onmatch="exclude" /> <!-- event_id:15 --> | ||
| 124 | <!--SYSMON EVENT ID 16 : SYSMON CONFIGURATION CHANGE, THIS LINE IS INCLUDED FOR DOCUMENTATION PURPOSES ONLY [ SYSMON 6.00+ ] --> | ||
| 125 | <!--SYSMON EVENT ID 17 : PIPE CREATED [ SYSMON 6.00+ ] --> | ||
| 126 | <!--SYSMON EVENT ID 18 : PIPE CONNECTED [ SYSMON 6.00+ ] --> | ||
| 127 | </EventFiltering> | ||
| 128 | </Sysmon> | ||
| 129 | EOF | ||
| 130 | ; | ||
| 131 | |||
| 132 | my $ft = Metabrik::File::Text->new_from_brik_init($self) or return; | ||
| 133 | $ft->append(0); | ||
| 134 | $ft->overwrite(1); | ||
| 135 | |||
| 136 | $ft->write($conf, $conf_file) or return; | ||
| 137 | |||
| 138 | return $conf_file; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub deploy { | ||
| 142 | my $self = shift; | ||
| 143 | my ($host, $user, $password) = @_; | ||
| 144 | |||
| 145 | $host ||= $self->host; | ||
| 146 | $user ||= $self->user; | ||
| 147 | $password ||= $self->password; | ||
| 148 | $self->brik_help_set_undef_arg('host', $host) or return; | ||
| 149 | $self->brik_help_set_undef_arg('user', $user) or return; | ||
| 150 | $self->brik_help_set_undef_arg('password', $password) or return; | ||
| 151 | |||
| 152 | my $sysmon_exe = $self->sysmon_exe; | ||
| 153 | my $remote_path = $self->remote_path; | ||
| 154 | |||
| 155 | my $full_path = $self->get_sysmon_exe or return; | ||
| 156 | |||
| 157 | $self->log->verbose("deploy: uploaded file [$full_path] to remote_path[$remote_path]"); | ||
| 158 | |||
| 159 | $self->upload($full_path) or return; | ||
| 160 | |||
| 161 | my $cmd = "\"cmd.exe /c $remote_path\\$sysmon_exe -i -accepteula -n\""; | ||
| 162 | |||
| 163 | return $self->execute($cmd); | ||
| 164 | } | ||
| 165 | |||
| 166 | sub undeploy { | ||
| 167 | my $self = shift; | ||
| 168 | my ($host, $user, $password) = @_; | ||
| 169 | |||
| 170 | $host ||= $self->host; | ||
| 171 | $user ||= $self->user; | ||
| 172 | $password ||= $self->password; | ||
| 173 | $self->brik_help_set_undef_arg('host', $host) or return; | ||
| 174 | $self->brik_help_set_undef_arg('user', $user) or return; | ||
| 175 | $self->brik_help_set_undef_arg('password', $password) or return; | ||
| 176 | |||
| 177 | my $sysmon_exe = $self->sysmon_exe; | ||
| 178 | my $remote_path = $self->remote_path; | ||
| 179 | |||
| 180 | $self->log->verbose("undeploy: from remote_path[$remote_path] ". | ||
| 181 | "sysmon_exe[$sysmon_exe]"); | ||
| 182 | |||
| 183 | my $cmd = "\"cmd.exe /c $remote_path\\$sysmon_exe -u\""; | ||
| 184 | |||
| 185 | return $self->execute($cmd); | ||
| 186 | } | ||
| 187 | |||
| 188 | sub update_conf { | ||
| 189 | my $self = shift; | ||
| 190 | my ($conf_file) = @_; | ||
| 191 | |||
| 192 | $conf_file ||= $self->conf_file; | ||
| 193 | $self->brik_help_run_undef_arg('update_conf', $conf_file) or return; | ||
| 194 | |||
| 195 | my $datadir = $self->datadir; | ||
| 196 | my $base_conf_file = $conf_file; | ||
| 197 | $conf_file = "$datadir/$conf_file"; | ||
| 198 | $self->brik_help_run_file_not_found('update_conf', $conf_file) or return; | ||
| 199 | |||
| 200 | my $sysmon_exe = $self->sysmon_exe; | ||
| 201 | my $remote_path = $self->remote_path; | ||
| 202 | |||
| 203 | my $full_path = $self->get_sysmon_exe or return; | ||
| 204 | |||
| 205 | $self->log->verbose("update_conf: uploaded file [$conf_file] ". | ||
| 206 | "to remote_path[$remote_path]"); | ||
| 207 | |||
| 208 | $self->upload($conf_file) or return; | ||
| 209 | |||
| 210 | my $cmd = "\"cmd.exe /c $remote_path\\$sysmon_exe -c $remote_path\\$base_conf_file\""; | ||
| 211 | |||
| 212 | return $self->execute($cmd); | ||
| 213 | } | ||
| 214 | |||
| 215 | sub redeploy { | ||
| 216 | my $self = shift; | ||
| 217 | |||
| 218 | $self->undeploy; | ||
| 219 | return $self->deploy; | ||
| 220 | } | ||
| 221 | |||
| 222 | 1; | ||
| 223 | |||
| 224 | __END__ | ||