| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # forensic::sysmon Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Forensic::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::Client::Elasticsearch::Query); |
|
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 | nodes => [ qw(node_list) ], # Inherited | ||
| 20 | index => [ qw(index) ], # Inherited | ||
| 21 | type => [ qw(type) ], # Inherited | ||
| 22 | filter_user => [ qw(user) ], | ||
| 23 | filter_session => [ qw(session) ], | ||
| 24 | filter_computer_name => [ qw(name) ], | ||
| 25 | use_regex_match => [ qw(0|1) ], | ||
| 26 | }, | ||
| 27 | attributes_default => { | ||
| 28 | index => 'winlogbeat-*', | ||
| 29 | type => 'wineventlog', | ||
| 30 | use_regex_match => 0, | ||
| 31 | }, | ||
| 32 | commands => { | ||
| 33 | create_client => [ ], | ||
| 34 | reset_client => [ ], | ||
| 35 | query => [ qw(query index|OPTIONAL type|OPTIONAL) ], | ||
| 36 | get_event_id => [ qw(event_id index|OPTIONAL type|OPTIONAL) ], | ||
| 37 | get_process_create => [ ], | ||
| 38 | get_file_creation_time_changed => [ ], | ||
| 39 | get_network_connection_detected => [ ], | ||
| 40 | get_sysmon_service_state_changed => [ ], | ||
| 41 | get_process_terminated => [ ], | ||
| 42 | get_driver_loaded => [ ], | ||
| 43 | get_image_loaded => [ ], | ||
| 44 | get_create_remote_thread => [ ], | ||
| 45 | get_raw_access_read_detected => [ ], | ||
| 46 | get_process_accessed => [ ], | ||
| 47 | get_file_created => [ ], | ||
| 48 | get_registry_object_added_or_deleted => [ ], | ||
| 49 | get_registry_value_set => [ ], | ||
| 50 | get_sysmon_config_state_changed => [ ], | ||
| 51 | list_file_created_processes => [ ], | ||
| 52 | ps => [ ], | ||
| 53 | ps_image_loaded => [ ], | ||
| 54 | ps_driver_loaded => [ ], | ||
| 55 | ps_parent_image => [ ], | ||
| 56 | ps_target_filename_created => [ ], | ||
| 57 | ps_target_filename_changed => [ ], | ||
| 58 | ps_target_image => [ ], | ||
| 59 | ps_network_connections => [ ], | ||
| 60 | ps_registry_object_added_or_deleted => [ ], | ||
| 61 | ps_registry_value_set => [ ], | ||
| 62 | ps_target_process_accessed => [ ], | ||
| 63 | list_users => [ ], | ||
| 64 | list_sessions => [ ], | ||
| 65 | list_computer_names => [ ], | ||
| 66 | list_domains => [ ], | ||
| 67 | build_list => [ qw(ps_data) ], | ||
| 68 | write_list => [ qw(list_data output_csv) ], | ||
| 69 | read_list => [ qw(input_csv) ], | ||
| 70 | clean_ps_from_list => [ qw(ps_data input_csv sources|OPTIONAL) ], | ||
| 71 | save_state => [ qw(ps_type|OPTIONAL) ], | ||
| 72 | diff_current_state => [ qw(ps_type|OPTIONAL sources|OPTIONAL) ], | ||
| 73 | }, | ||
| 74 | require_modules => { | ||
| 75 | 'Metabrik::File::Csv' => [ ], | ||
| 76 | }, | ||
| 77 | require_binaries => { | ||
| 78 | }, | ||
| 79 | optional_binaries => { | ||
| 80 | }, | ||
| 81 | need_packages => { | ||
| 82 | }, | ||
| 83 | }; | ||
| 84 | } | ||
| 85 | |||
| 86 | # | ||
| 87 | # 1: PROCESS CREATION | ||
| 88 | # 2: FILE CREATION TIME RETROACTIVELY CHANGED IN THE FILESYSTEM | ||
| 89 | # 3: NETWORK CONNECTION INITIATED | ||
| 90 | # 4: RESERVED FOR SYSMON STATUS MESSAGES | ||
| 91 | # 5: PROCESS ENDED | ||
| 92 | # 6: DRIVER LOADED INTO KERNEL | ||
| 93 | # 7: DLL (IMAGE) LOADED BY PROCESS | ||
| 94 | # 8: REMOTE THREAD CREATED | ||
| 95 | # 9: RAW DISK ACCESS | ||
| 96 | # 10: INTER-PROCESS ACCESS | ||
| 97 | # 11: FILE CREATED | ||
| 98 | # 12: REGISTRY MODIFICATION | ||
| 99 | # 13: REGISTRY MODIFICATION | ||
| 100 | # 14: REGISTRY MODIFICATION | ||
| 101 | # 15: ALTERNATE DATA STREAM CREATED | ||
| 102 | # 16: SYSMON CONFIGURATION CHANGE | ||
| 103 | # 17: PIPE CREATED | ||
| 104 | # 18: PIPE CONNECTED | ||
| 105 | # | ||
| 106 | sub get_event_id { | ||
| 107 | my $self = shift; | ||
| 108 | my ($event_id, $index, $type) = @_; | ||
| 109 | |||
| 110 | $index ||= $self->index; | ||
| 111 | $type ||= $self->type; | ||
| 112 | $self->brik_help_run_undef_arg('get_event_id', $event_id) or return; | ||
| 113 | |||
| 114 | my $user = $self->filter_user; | ||
| 115 | my $name = $self->filter_computer_name; | ||
| 116 | my $session = $self->filter_session; | ||
| 117 | |||
| 118 | my $from = 0; | ||
| 119 | my $size = 10_000; | ||
| 120 | my $q = { | ||
| 121 | from => $from, | ||
| 122 | size => $size, | ||
| 123 | sort => [ | ||
| 124 | { '@timestamp' => { order => "desc" } }, | ||
| 125 | ], | ||
| 126 | query => { | ||
| 127 | bool => { | ||
| 128 | must => [ | ||
| 129 | { term => { event_id => $event_id } }, | ||
| 130 | { term => { source_name => 'Microsoft-Windows-Sysmon' } } | ||
| 131 | ] | ||
| 132 | } | ||
| 133 | } | ||
| 134 | }; | ||
| 135 | |||
| 136 | if (defined($user)) { | ||
| 137 | push @{$q->{query}{bool}{must}}, { term => { 'event_data.User' => $user } }; | ||
| 138 | } | ||
| 139 | if (defined($name)) { | ||
| 140 | push @{$q->{query}{bool}{must}}, { term => { 'computer_name' => $name } }; | ||
| 141 | } | ||
| 142 | if (defined($session)) { | ||
| 143 | push @{$q->{query}{bool}{must}}, | ||
| 144 | { term => { 'event_data.LogonGuid' => $session } }; | ||
| 145 | } | ||
| 146 | |||
| 147 | my $r = $self->query($q, $index, $type); | ||
| 148 | my $hits = $self->get_query_result_hits($r); | ||
| 149 | |||
| 150 | my @list = (); | ||
| 151 | for my $this (@$hits) { | ||
| 152 | $this = $this->{_source}; | ||
| 153 | push @list, { | ||
| 154 | '@timestamp' => $this->{'@timestamp'}, | ||
| 155 | event_id => $this->{event_id}, | ||
| 156 | event_data => $this->{event_data}, | ||
| 157 | computer_name => $this->{computer_name}, | ||
| 158 | process_id => $this->{process_id}, | ||
| 159 | provider_guid => $this->{provider_guid}, | ||
| 160 | record_number => $this->{record_number}, | ||
| 161 | thread_id => $this->{thread_id}, | ||
| 162 | task => $this->{task}, | ||
| 163 | user => $this->{user}, | ||
| 164 | version => $this->{version}, | ||
| 165 | }; | ||
| 166 | } | ||
| 167 | |||
| 168 | return \@list; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub get_process_create { | ||
| 172 | my $self = shift; | ||
| 173 | my ($index, $type) = @_; | ||
| 174 | |||
| 175 | $index ||= $self->index; | ||
| 176 | $type ||= $self->type; | ||
| 177 | |||
| 178 | return $self->get_event_id(1, $index, $type); | ||
| 179 | } | ||
| 180 | |||
| 181 | sub get_file_creation_time_changed { | ||
| 182 | my $self = shift; | ||
| 183 | my ($index, $type) = @_; | ||
| 184 | |||
| 185 | $index ||= $self->index; | ||
| 186 | $type ||= $self->type; | ||
| 187 | |||
| 188 | return $self->get_event_id(2, $index, $type); | ||
| 189 | } | ||
| 190 | |||
| 191 | sub get_network_connection_detected { | ||
| 192 | my $self = shift; | ||
| 193 | my ($index, $type) = @_; | ||
| 194 | |||
| 195 | $index ||= $self->index; | ||
| 196 | $type ||= $self->type; | ||
| 197 | |||
| 198 | return $self->get_event_id(3, $index, $type); | ||
| 199 | } | ||
| 200 | |||
| 201 | sub get_sysmon_service_state_changed { | ||
| 202 | my $self = shift; | ||
| 203 | my ($index, $type) = @_; | ||
| 204 | |||
| 205 | $index ||= $self->index; | ||
| 206 | $type ||= $self->type; | ||
| 207 | |||
| 208 | return $self->get_event_id(4, $index, $type); | ||
| 209 | } | ||
| 210 | |||
| 211 | sub get_process_terminated { | ||
| 212 | my $self = shift; | ||
| 213 | my ($index, $type) = @_; | ||
| 214 | |||
| 215 | $index ||= $self->index; | ||
| 216 | $type ||= $self->type; | ||
| 217 | |||
| 218 | return $self->get_event_id(5, $index, $type); | ||
| 219 | } | ||
| 220 | |||
| 221 | sub get_driver_loaded { | ||
| 222 | my $self = shift; | ||
| 223 | my ($index, $type) = @_; | ||
| 224 | |||
| 225 | $index ||= $self->index; | ||
| 226 | $type ||= $self->type; | ||
| 227 | |||
| 228 | return $self->get_event_id(6, $index, $type); | ||
| 229 | } | ||
| 230 | |||
| 231 | sub get_image_loaded { | ||
| 232 | my $self = shift; | ||
| 233 | my ($index, $type) = @_; | ||
| 234 | |||
| 235 | $index ||= $self->index; | ||
| 236 | $type ||= $self->type; | ||
| 237 | |||
| 238 | return $self->get_event_id(7, $index, $type); | ||
| 239 | } | ||
| 240 | |||
| 241 | sub get_create_remote_thread { | ||
| 242 | my $self = shift; | ||
| 243 | my ($index, $type) = @_; | ||
| 244 | |||
| 245 | $index ||= $self->index; | ||
| 246 | $type ||= $self->type; | ||
| 247 | |||
| 248 | return $self->get_event_id(8, $index, $type); | ||
| 249 | } | ||
| 250 | |||
| 251 | sub get_raw_access_read_detected { | ||
| 252 | my $self = shift; | ||
| 253 | my ($index, $type) = @_; | ||
| 254 | |||
| 255 | $index ||= $self->index; | ||
| 256 | $type ||= $self->type; | ||
| 257 | |||
| 258 | return $self->get_event_id(9, $index, $type); | ||
| 259 | } | ||
| 260 | |||
| 261 | sub get_process_accessed { | ||
| 262 | my $self = shift; | ||
| 263 | my ($index, $type) = @_; | ||
| 264 | |||
| 265 | $index ||= $self->index; | ||
| 266 | $type ||= $self->type; | ||
| 267 | |||
| 268 | return $self->get_event_id(10, $index, $type); | ||
| 269 | } | ||
| 270 | |||
| 271 | sub get_file_created { | ||
| 272 | my $self = shift; | ||
| 273 | my ($index, $type) = @_; | ||
| 274 | |||
| 275 | $index ||= $self->index; | ||
| 276 | $type ||= $self->type; | ||
| 277 | |||
| 278 | return $self->get_event_id(11, $index, $type); | ||
| 279 | } | ||
| 280 | |||
| 281 | sub get_registry_object_added_or_deleted { | ||
| 282 | my $self = shift; | ||
| 283 | my ($index, $type) = @_; | ||
| 284 | |||
| 285 | $index ||= $self->index; | ||
| 286 | $type ||= $self->type; | ||
| 287 | |||
| 288 | return $self->get_event_id(12, $index, $type); | ||
| 289 | } | ||
| 290 | |||
| 291 | sub get_registry_value_set { | ||
| 292 | my $self = shift; | ||
| 293 | my ($index, $type) = @_; | ||
| 294 | |||
| 295 | $index ||= $self->index; | ||
| 296 | $type ||= $self->type; | ||
| 297 | |||
| 298 | return $self->get_event_id(13, $index, $type); | ||
| 299 | } | ||
| 300 | |||
| 301 | # XXX: 14 | ||
| 302 | # XXX: 15 | ||
| 303 | |||
| 304 | sub get_sysmon_config_state_changed { | ||
| 305 | my $self = shift; | ||
| 306 | my ($index, $type) = @_; | ||
| 307 | |||
| 308 | $index ||= $self->index; | ||
| 309 | $type ||= $self->type; | ||
| 310 | |||
| 311 | return $self->get_event_id(16, $index, $type); | ||
| 312 | } | ||
| 313 | |||
| 314 | sub _read_hashes { | ||
| 315 | my $self = shift; | ||
| 316 | my ($hashes) = @_; | ||
| 317 | |||
| 318 | # SHA1=99052FD84F00B5279E304798F5C2675A1C201146, | ||
| 319 | # MD5=70C298C6990F5A0BBF60F5C035BAA0B9, | ||
| 320 | # SHA256=D4E8D0DCAF077A4FECA5C974EA430A2AD1FE3118F14512D662B26D8D09CD3A08, | ||
| 321 | # IMPHASH=089C9EDE118FC9F36EEBA769ACA5EA16 | ||
| 322 | my $h = {}; | ||
| 323 | my @hash_list = split(/,/, $hashes); | ||
| 324 | for (@hash_list) { | ||
| 325 | if (m{^.+=.+$}) { | ||
| 326 | my ($k, $v) = split(/=/, $_); | ||
| 327 | if (defined($k) && defined($v)) { | ||
| 328 | $h->{lc($k)} = lc($v); | ||
| 329 | } | ||
| 330 | } | ||
| 331 | } | ||
| 332 | |||
| 333 | return $h; | ||
| 334 | } | ||
| 335 | |||
| 336 | sub _ps { | ||
| 337 | my $self = shift; | ||
| 338 | |||
| 339 | my $r = $self->get_process_create or return; | ||
| 340 | |||
| 341 | my @ps = (); | ||
| 342 | for my $this (@$r) { | ||
| 343 | my $process_id = $this->{event_data}{ProcessId}; | ||
| 344 | my $image = lc($this->{event_data}{Image}); | ||
| 345 | my $command_line = lc($this->{event_data}{CommandLine}); | ||
| 346 | my $parent_process_id = $this->{event_data}{ParentProcessId}; | ||
| 347 | my $parent_image = lc($this->{event_data}{ParentImage}); | ||
| 348 | my $parent_command_line = lc($this->{event_data}{ParentCommandLine}); | ||
| 349 | |||
| 350 | my $new = { | ||
| 351 | #process_id => $process_id, | ||
| 352 | image => $self->_fix_path(lc($image)), | ||
| 353 | #command_line => $self->_fix_path(lc($command_line)), | ||
| 354 | #parent_process_id => $parent_process_id, | ||
| 355 | parent_image => $self->_fix_path(lc($parent_image)), | ||
| 356 | #parent_command_line => $self->_fix_path(lc($parent_command_line)), | ||
| 357 | }; | ||
| 358 | |||
| 359 | my $hashes = $this->{event_data}{Hashes}; | ||
| 360 | my $h = $self->_read_hashes($hashes); | ||
| 361 | for my $k (keys %$h) { | ||
| 362 | $new->{$k} = $h->{$k}; | ||
| 363 | } | ||
| 364 | |||
| 365 | push @ps, $new; | ||
| 366 | } | ||
| 367 | |||
| 368 | return \@ps; | ||
| 369 | } | ||
| 370 | |||
| 371 | sub _dedup_values { | ||
| 372 | my $self = shift; | ||
| 373 | my ($data, $value) = @_; | ||
| 374 | |||
| 375 | $value ||= 'source'; | ||
| 376 | |||
| 377 | for my $k1 (keys %$data) { | ||
| 378 | for my $k2 (keys %{$data->{$k1}}) { | ||
| 379 | my $ary = $data->{$k1}{$k2}; | ||
| 380 | if (ref($ary) eq 'ARRAY') { | ||
| 381 | my %uniq = map { $_ => 1 } @$ary; | ||
| 382 | $data->{$k1}{$k2} = [ sort { $a cmp $b } keys %uniq ]; | ||
| 383 | } | ||
| 384 | } | ||
| 385 | } | ||
| 386 | |||
| 387 | my @list = (); | ||
| 388 | for my $k1 (keys %$data) { | ||
| 389 | push @list, { $value => $k1, %{$data->{$k1}} }; | ||
| 390 | } | ||
| 391 | |||
| 392 | return \@list; | ||
| 393 | } | ||
| 394 | |||
| 395 | sub _fix_path { | ||
| 396 | my $self = shift; | ||
| 397 | my ($path) = @_; | ||
| 398 | |||
| 399 | $path =~ s{\\}{/}g; | ||
| 400 | |||
| 401 | return $path; | ||
| 402 | } | ||
| 403 | |||
| 404 | sub list_file_created_processes { | ||
| 405 | my $self = shift; | ||
| 406 | my ($index, $type) = @_; | ||
| 407 | |||
| 408 | my $r = $self->get_file_created or return; | ||
| 409 | |||
| 410 | my %list = (); | ||
| 411 | for my $this (@$r) { | ||
| 412 | my $image = $self->_fix_path(lc($this->{event_data}{Image})); | ||
| 413 | $list{$image}++; | ||
| 414 | } | ||
| 415 | |||
| 416 | return [ sort { $a cmp $b } keys %list ]; | ||
| 417 | } | ||
| 418 | |||
| 419 | sub ps { | ||
| 420 | my $self = shift; | ||
| 421 | |||
| 422 | my $r = $self->get_process_create or return; | ||
| 423 | |||
| 424 | my %ps = (); | ||
| 425 | for my $this (@$r) { | ||
| 426 | my $source = $self->_fix_path(lc($this->{event_data}{ParentImage})); | ||
| 427 | my $target = $self->_fix_path(lc($this->{event_data}{Image})); | ||
| 428 | |||
| 429 | push @{$ps{$source}{targets}}, $target; | ||
| 430 | } | ||
| 431 | |||
| 432 | return $self->_dedup_values(\%ps); | ||
| 433 | } | ||
| 434 | |||
| 435 | sub ps_image_loaded { | ||
| 436 | my $self = shift; | ||
| 437 | |||
| 438 | my $r = $self->get_image_loaded or return; | ||
| 439 | |||
| 440 | my %ps = (); | ||
| 441 | for my $this (@$r) { | ||
| 442 | my $source = $self->_fix_path(lc($this->{event_data}{Image})); | ||
| 443 | my $target = $self->_fix_path(lc($this->{event_data}{ImageLoaded})); | ||
| 444 | |||
| 445 | push @{$ps{$source}{targets}}, $target; | ||
| 446 | } | ||
| 447 | |||
| 448 | return $self->_dedup_values(\%ps); | ||
| 449 | } | ||
| 450 | |||
| 451 | sub ps_driver_loaded { | ||
| 452 | my $self = shift; | ||
| 453 | |||
| 454 | my $r = $self->get_driver_loaded or return; | ||
| 455 | |||
| 456 | my %ps = (); | ||
| 457 | for my $this (@$r) { | ||
| 458 | my $source = $self->_fix_path(lc($this->{event_data}{ImageLoaded})); | ||
| 459 | my $target = $this->{event_data}{Hashes}; | ||
| 460 | |||
| 461 | push @{$ps{$source}{targets}}, $target; | ||
| 462 | } | ||
| 463 | |||
| 464 | return $self->_dedup_values(\%ps); | ||
| 465 | } | ||
| 466 | |||
| 467 | sub ps_parent_image { | ||
| 468 | my $self = shift; | ||
| 469 | |||
| 470 | my $r = $self->get_process_create or return; | ||
| 471 | |||
| 472 | my %ps = (); | ||
| 473 | for my $this (@$r) { | ||
| 474 | my $source = $self->_fix_path(lc($this->{event_data}{Image})); | ||
| 475 | my $target = $self->_fix_path(lc($this->{event_data}{ParentImage})); | ||
| 476 | |||
| 477 | push @{$ps{$source}{targets}}, $target; | ||
| 478 | } | ||
| 479 | |||
| 480 | return $self->_dedup_values(\%ps); | ||
| 481 | } | ||
| 482 | |||
| 483 | sub ps_target_filename_created { | ||
| 484 | my $self = shift; | ||
| 485 | |||
| 486 | my $r = $self->get_file_created or return; | ||
| 487 | |||
| 488 | my %ps = (); | ||
| 489 | for my $this (@$r) { | ||
| 490 | my $source = $self->_fix_path(lc($this->{event_data}{Image})); | ||
| 491 | my $target = $self->_fix_path(lc($this->{event_data}{TargetFilename})); | ||
| 492 | |||
| 493 | push @{$ps{$source}{targets}}, $target; | ||
| 494 | } | ||
| 495 | |||
| 496 | return $self->_dedup_values(\%ps); | ||
| 497 | } | ||
| 498 | |||
| 499 | sub ps_target_filename_changed { | ||
| 500 | my $self = shift; | ||
| 501 | |||
| 502 | my $r = $self->get_file_creation_time_changed or return; | ||
| 503 | |||
| 504 | my %ps = (); | ||
| 505 | for my $this (@$r) { | ||
| 506 | my $source = $self->_fix_path(lc($this->{event_data}{Image})); | ||
| 507 | my $target = $self->_fix_path(lc($this->{event_data}{TargetFilename})); | ||
| 508 | |||
| 509 | push @{$ps{$source}{targets}}, $target; | ||
| 510 | } | ||
| 511 | |||
| 512 | return $self->_dedup_values(\%ps); | ||
| 513 | } | ||
| 514 | |||
| 515 | sub ps_target_image { | ||
| 516 | my $self = shift; | ||
| 517 | |||
| 518 | my $r = $self->get_create_remote_thread or return; | ||
| 519 | |||
| 520 | my %ps = (); | ||
| 521 | for my $this (@$r) { | ||
| 522 | my $source = $self->_fix_path(lc($this->{event_data}{SourceImage})); | ||
| 523 | my $target = $self->_fix_path(lc($this->{event_data}{TargetImage})); | ||
| 524 | |||
| 525 | push @{$ps{$source}{targets}}, $target; | ||
| 526 | } | ||
| 527 | |||
| 528 | return $self->_dedup_values(\%ps); | ||
| 529 | } | ||
| 530 | |||
| 531 | sub ps_network_connections { | ||
| 532 | my $self = shift; | ||
| 533 | |||
| 534 | my $r = $self->get_network_connection_detected or return; | ||
| 535 | |||
| 536 | my %ps = (); | ||
| 537 | for my $this (@$r) { | ||
| 538 | my $source = $self->_fix_path(lc($this->{event_data}{Image})); | ||
| 539 | my $src_ip = $this->{event_data}{SourceIp}; | ||
| 540 | my $src_hostname = $this->{event_data}{SourceHostname} || ''; | ||
| 541 | my $dest_ip = $this->{event_data}{DestinationIp}; | ||
| 542 | my $dest_hostname = $this->{event_data}{DestinationHostname} || ''; | ||
| 543 | my $src_port = $this->{event_data}{SourcePort}; | ||
| 544 | my $dest_port = $this->{event_data}{DestinationPort}; | ||
| 545 | my $protocol = lc($this->{event_data}{Protocol}); | ||
| 546 | |||
| 547 | my $target = "$protocol|[$src_ip]:$src_port:$src_hostname>". | ||
| 548 | "[$dest_ip]:$dest_port:$dest_hostname"; | ||
| 549 | |||
| 550 | push @{$ps{$source}{targets}}, $target; | ||
| 551 | } | ||
| 552 | |||
| 553 | return $self->_dedup_values(\%ps); | ||
| 554 | } | ||
| 555 | |||
| 556 | sub ps_registry_object_added_or_deleted { | ||
| 557 | my $self = shift; | ||
| 558 | |||
| 559 | my $r = $self->get_registry_object_added_or_deleted or return; | ||
| 560 | |||
| 561 | my %ps = (); | ||
| 562 | for my $this (@$r) { | ||
| 563 | my $source = $self->_fix_path(lc($this->{event_data}{Image})); | ||
| 564 | my $target = $self->_fix_path($this->{event_data}{TargetObject}); | ||
| 565 | |||
| 566 | push @{$ps{$source}{targets}}, $target; | ||
| 567 | } | ||
| 568 | |||
| 569 | return $self->_dedup_values(\%ps); | ||
| 570 | } | ||
| 571 | |||
| 572 | sub ps_registry_value_set { | ||
| 573 | my $self = shift; | ||
| 574 | |||
| 575 | my $r = $self->get_registry_value_set or return; | ||
| 576 | |||
| 577 | my %ps = (); | ||
| 578 | for my $this (@$r) { | ||
| 579 | my $source = $self->_fix_path(lc($this->{event_data}{Image})); | ||
| 580 | my $target = $self->_fix_path($this->{event_data}{TargetObject}); | ||
| 581 | |||
| 582 | push @{$ps{$source}{targets}}, $target; | ||
| 583 | } | ||
| 584 | |||
| 585 | return $self->_dedup_values(\%ps); | ||
| 586 | } | ||
| 587 | |||
| 588 | sub ps_target_process_accessed { | ||
| 589 | my $self = shift; | ||
| 590 | |||
| 591 | my $r = $self->get_process_accessed or return; | ||
| 592 | |||
| 593 | my %ps = (); | ||
| 594 | for my $this (@$r) { | ||
| 595 | my $source = $self->_fix_path(lc($this->{event_data}{SourceImage})); | ||
| 596 | my $target = $self->_fix_path(lc($this->{event_data}{TargetImage})); | ||
| 597 | |||
| 598 | push @{$ps{$source}{targets}}, $target; | ||
| 599 | } | ||
| 600 | |||
| 601 | return $self->_dedup_values(\%ps); | ||
| 602 | } | ||
| 603 | |||
| 604 | sub list_users { | ||
| 605 | my $self = shift; | ||
| 606 | |||
| 607 | my $r = $self->unique_values('event_data.User') or return; | ||
| 608 | |||
| 609 | my %h = (); | ||
| 610 | if (exists($r->{aggregations}) | ||
| 611 | && exists($r->{aggregations}{1}) | ||
| 612 | && exists($r->{aggregations}{1}{buckets})) { | ||
| 613 | my $buckets = $r->{aggregations}{1}{buckets}; | ||
| 614 | for (@$buckets) { | ||
| 615 | $h{$_->{key}}++; | ||
| 616 | } | ||
| 617 | } | ||
| 618 | |||
| 619 | return [ sort { $a cmp $b} keys %h ]; | ||
| 620 | } | ||
| 621 | |||
| 622 | sub list_sessions { | ||
| 623 | my $self = shift; | ||
| 624 | |||
| 625 | my $r = $self->unique_values('event_data.LogonGuid') or return; | ||
| 626 | |||
| 627 | my %h = (); | ||
| 628 | if (exists($r->{aggregations}) | ||
| 629 | && exists($r->{aggregations}{1}) | ||
| 630 | && exists($r->{aggregations}{1}{buckets})) { | ||
| 631 | my $buckets = $r->{aggregations}{1}{buckets}; | ||
| 632 | for (@$buckets) { | ||
| 633 | $h{$_->{key}}++; | ||
| 634 | } | ||
| 635 | } | ||
| 636 | |||
| 637 | return [ sort { $a cmp $b} keys %h ]; | ||
| 638 | } | ||
| 639 | |||
| 640 | sub list_computer_names { | ||
| 641 | my $self = shift; | ||
| 642 | |||
| 643 | my $r = $self->unique_values('computer_name') or return; | ||
| 644 | |||
| 645 | my %h = (); | ||
| 646 | if (exists($r->{aggregations}) | ||
| 647 | && exists($r->{aggregations}{1}) | ||
| 648 | && exists($r->{aggregations}{1}{buckets})) { | ||
| 649 | my $buckets = $r->{aggregations}{1}{buckets}; | ||
| 650 | for (@$buckets) { | ||
| 651 | $h{$_->{key}}++; | ||
| 652 | } | ||
| 653 | } | ||
| 654 | |||
| 655 | return [ sort { $a cmp $b} keys %h ]; | ||
| 656 | } | ||
| 657 | |||
| 658 | sub list_domains { | ||
| 659 | my $self = shift; | ||
| 660 | |||
| 661 | my $r = $self->unique_values('user.domain') or return; | ||
| 662 | |||
| 663 | my %h = (); | ||
| 664 | if (exists($r->{aggregations}) | ||
| 665 | && exists($r->{aggregations}{1}) | ||
| 666 | && exists($r->{aggregations}{1}{buckets})) { | ||
| 667 | my $buckets = $r->{aggregations}{1}{buckets}; | ||
| 668 | for (@$buckets) { | ||
| 669 | $h{$_->{key}}++; | ||
| 670 | } | ||
| 671 | } | ||
| 672 | |||
| 673 | return [ sort { $a cmp $b} keys %h ]; | ||
| 674 | } | ||
| 675 | |||
| 676 | sub build_list { | ||
| 677 | my $self = shift; | ||
| 678 | my ($data) = @_; | ||
| 679 | |||
| 680 | $self->brik_help_run_undef_arg('build_list', $data) or return; | ||
| 681 | $self->brik_help_run_invalid_arg('build_list', $data, 'ARRAY') or return; | ||
| 682 | |||
| 683 | # First, search which keys are multi-valued | ||
| 684 | my %a_keys = (); | ||
| 685 | my %s_keys = (); | ||
| 686 | for my $this (@$data) { | ||
| 687 | for my $k (keys %$this) { | ||
| 688 | if (ref($this->{$k}) eq 'ARRAY') { | ||
| 689 | $a_keys{$k}++; | ||
| 690 | } | ||
| 691 | elsif (ref($this->{$k}) eq 'HASH') { | ||
| 692 | $self->log->warning("build_list: uncaught data, skipping"); | ||
| 693 | } | ||
| 694 | elsif (ref($this->{$k}) eq '') { | ||
| 695 | $s_keys{$k}++; | ||
| 696 | } | ||
| 697 | } | ||
| 698 | } | ||
| 699 | |||
| 700 | # More than one key with multi-valued, case not handled yet. | ||
| 701 | if (keys %a_keys > 1) { | ||
| 702 | return $self->log->error("build_list: unable to process data"); | ||
| 703 | } | ||
| 704 | |||
| 705 | my @list = (); | ||
| 706 | for my $this (@$data) { | ||
| 707 | my $new = {}; | ||
| 708 | for my $s (keys %s_keys) { | ||
| 709 | $new->{$s} = $this->{$s}; | ||
| 710 | } | ||
| 711 | if (keys %a_keys > 0) { | ||
| 712 | my $sav = { %$new }; | ||
| 713 | for my $a (keys %a_keys) { | ||
| 714 | for (@{$this->{$a}}) { | ||
| 715 | $new->{$a} = $_; | ||
| 716 | push @list, $new; | ||
| 717 | $new = { %$sav }; | ||
| 718 | } | ||
| 719 | } | ||
| 720 | } | ||
| 721 | else { | ||
| 722 | push @list, $new; | ||
| 723 | } | ||
| 724 | } | ||
| 725 | |||
| 726 | return \@list; | ||
| 727 | } | ||
| 728 | |||
| 729 | sub write_list { | ||
| 730 | my $self = shift; | ||
| 731 | my ($data, $output) = @_; | ||
| 732 | |||
| 733 | $self->brik_help_run_undef_arg('write_list', $data) or return; | ||
| 734 | $self->brik_help_run_invalid_arg('write_list', $data, 'ARRAY') or return; | ||
| 735 | $self->brik_help_run_undef_arg('write_list', $output) or return; | ||
| 736 | |||
| 737 | my $fc = Metabrik::File::Csv->new_from_brik_init($self) or return; | ||
| 738 | $fc->use_quoting(1); | ||
| 739 | $fc->overwrite(1); | ||
| 740 | $fc->append(0); | ||
| 741 | |||
| 742 | # Escape some chars so we can use regexes | ||
| 743 | for my $this (@$data) { | ||
| 744 | for my $k (keys %$this) { | ||
| 745 | $this->{$k} =~ s{\|}{\\|}g; | ||
| 746 | $this->{$k} =~ s{\.}{\\.}g; | ||
| 747 | $this->{$k} =~ s{\?}{\\?}g; | ||
| 748 | $this->{$k} =~ s{\(}{\\(}g; | ||
| 749 | $this->{$k} =~ s{\)}{\\)}g; | ||
| 750 | $this->{$k} =~ s{\*}{\\*}g; | ||
| 751 | $this->{$k} =~ s{\[}{\\[}g; | ||
| 752 | $this->{$k} =~ s{\]}{\\]}g; | ||
| 753 | $this->{$k} =~ s/{/\\{/g; | ||
| 754 | $this->{$k} =~ s/}/\\}/g; | ||
| 755 | $this->{$k} =~ s{^}{\^}; | ||
| 756 | $this->{$k} =~ s{$}{\$}; | ||
| 757 | } | ||
| 758 | } | ||
| 759 | |||
| 760 | $fc->write($data, $output) or return; | ||
| 761 | |||
| 762 | return $output; | ||
| 763 | } | ||
| 764 | |||
| 765 | sub read_list { | ||
| 766 | my $self = shift; | ||
| 767 | my ($input) = @_; | ||
| 768 | |||
| 769 | $self->brik_help_run_undef_arg('read_list', $input) or return; | ||
| 770 | $self->brik_help_run_file_not_found('read_list', $input) or return; | ||
| 771 | |||
| 772 | my $fc = Metabrik::File::Csv->new_from_brik_init($self) or return; | ||
| 773 | $fc->use_quoting(1); | ||
| 774 | |||
| 775 | return $fc->read($input); | ||
| 776 | } | ||
| 777 | |||
| 778 | sub clean_ps_from_list { | ||
| 779 | my $self = shift; | ||
| 780 | my ($data, $input, $sources) = @_; | ||
| 781 | |||
| 782 | $self->brik_help_run_undef_arg('clean_ps_from_list', $data) or return; | ||
| 783 | $self->brik_help_run_invalid_arg('clean_ps_from_list', $data, 'ARRAY') or return; | ||
| 784 | $self->brik_help_run_undef_arg('clean_ps_from_list', $input) or return; | ||
| 785 | $self->brik_help_run_file_not_found('clean_ps_from_list', $input) or return; | ||
| 786 | |||
| 787 | if (defined($sources)) { | ||
| 788 | $self->brik_help_run_invalid_arg('clean_ps_from_list', $sources, 'ARRAY') | ||
| 789 | or return; | ||
| 790 | } | ||
| 791 | |||
| 792 | my $csv_list = $self->read_list($input) or return; | ||
| 793 | my $data_list = $self->build_list($data) or return; | ||
| 794 | |||
| 795 | my $first = $csv_list->[0]; | ||
| 796 | if (! defined($first)) { | ||
| 797 | return $self->log->error("clean_ps_from_list: empty [$input] file?"); | ||
| 798 | } | ||
| 799 | my @keys = keys %$first; | ||
| 800 | my $count = scalar @keys; | ||
| 801 | |||
| 802 | #my $wl_remove = qr/(?:\\|^\^|\$$)/; | ||
| 803 | my $wl_remove_start = qr/^\^/; | ||
| 804 | my $wl_remove_end = qr/\$$/; | ||
| 805 | my $wl_remove_pipe = qr/\\|$/; | ||
| 806 | |||
| 807 | my @clean = (); | ||
| 808 | for my $ps (@$data_list) { | ||
| 809 | if (defined($sources)) { | ||
| 810 | my $skip = 1; | ||
| 811 | for (@$sources) { | ||
| 812 | if ($ps->{source} eq $_) { | ||
| 813 | $skip = 0; | ||
| 814 | last; | ||
| 815 | } | ||
| 816 | } | ||
| 817 | next if $skip; | ||
| 818 | } | ||
| 819 | my $whitelisted = 0; | ||
| 820 | for my $csv (@$csv_list) { | ||
| 821 | my $this_count = 0; | ||
| 822 | for my $k (@keys) { | ||
| 823 | my $v = $ps->{$k}; | ||
| 824 | my $wl = $csv->{$k}; | ||
| 825 | if ($self->use_regex_match) { | ||
| 826 | if ($v =~ m{^$wl$}) { | ||
| 827 | $this_count++; | ||
| 828 | } | ||
| 829 | } | ||
| 830 | else { | ||
| 831 | # We have to remove escape chars. | ||
| 832 | #$wl =~ s{$wl_remove}{}g; | ||
| 833 | #$wl =~ s{\\|)}{}g; | ||
| 834 | #$wl =~ s{^\^}{}g; | ||
| 835 | #$wl =~ s{\$$}{}g; | ||
| 836 | $wl =~ s{$wl_remove_start}{}g; | ||
| 837 | $wl =~ s{$wl_remove_end}{}g; | ||
| 838 | $wl =~ s{$wl_remove_pipe}{}g; | ||
| 839 | #print "compare [$v] vs [$wl]\n"; | ||
| 840 | if ($v eq $wl) { | ||
| 841 | $this_count++; | ||
| 842 | } | ||
| 843 | } | ||
| 844 | } | ||
| 845 | if ($this_count == $count) { # Whitelist matched | ||
| 846 | $whitelisted = 1; | ||
| 847 | #print "Whitelisted\n"; | ||
| 848 | last; | ||
| 849 | } | ||
| 850 | } | ||
| 851 | if (! $whitelisted) { | ||
| 852 | push @clean, $ps; | ||
| 853 | } | ||
| 854 | } | ||
| 855 | |||
| 856 | return \@clean; | ||
| 857 | } | ||
| 858 | |||
| 859 | sub save_state { | ||
| 860 | my $self = shift; | ||
| 861 | my ($type) = @_; | ||
| 862 | |||
| 863 | my @ps = qw( | ||
| 864 | ps | ||
| 865 | ps_driver_loaded | ||
| 866 | ps_image_loaded | ||
| 867 | ps_network_connections | ||
| 868 | ps_parent_image | ||
| 869 | ps_registry_object_added_or_deleted | ||
| 870 | ps_registry_value_set | ||
| 871 | ps_target_filename_changed | ||
| 872 | ps_target_filename_created | ||
| 873 | ps_target_image | ||
| 874 | ps_target_process_accessed | ||
| 875 | ); | ||
| 876 | |||
| 877 | # Process only one type | ||
| 878 | if (defined($type)) { | ||
| 879 | @ps = ( $type ); | ||
| 880 | } | ||
| 881 | |||
| 882 | for my $this (@ps) { | ||
| 883 | $self->log->info("save_state: saving state for type [$this] to [$this.csv]..."); | ||
| 884 | |||
| 885 | my $ps = $self->$this; | ||
| 886 | if (! defined($ps)) { | ||
| 887 | $self->log->error("save_state: failed [$this], skipping"); | ||
| 888 | next; | ||
| 889 | } | ||
| 890 | |||
| 891 | my $list = $self->build_list($ps); | ||
| 892 | if (! defined($list)) { | ||
| 893 | $self->log->error("save_state: failed build_list, skipping"); | ||
| 894 | next; | ||
| 895 | } | ||
| 896 | |||
| 897 | my $r = $self->write_list($list, "$this.csv"); | ||
| 898 | if (! defined($r)) { | ||
| 899 | $self->log->error("save_state: failed write_list, skipping"); | ||
| 900 | next; | ||
| 901 | } | ||
| 902 | } | ||
| 903 | |||
| 904 | return 1; | ||
| 905 | } | ||
| 906 | |||
| 907 | sub diff_current_state { | ||
| 908 | my $self = shift; | ||
| 909 | my ($type, $sources) = @_; | ||
| 910 | |||
| 911 | if (defined($sources)) { | ||
| 912 | $self->brik_help_run_invalid_arg('diff_current_state', $sources, 'ARRAY') | ||
| 913 | or return; | ||
| 914 | } | ||
| 915 | |||
| 916 | my @ps = qw( | ||
| 917 | ps | ||
| 918 | ps_driver_loaded | ||
| 919 | ps_image_loaded | ||
| 920 | ps_network_connections | ||
| 921 | ps_parent_image | ||
| 922 | ps_registry_object_added_or_deleted | ||
| 923 | ps_registry_value_set | ||
| 924 | ps_target_filename_changed | ||
| 925 | ps_target_filename_created | ||
| 926 | ps_target_image | ||
| 927 | ps_target_process_accessed | ||
| 928 | ); | ||
| 929 | |||
| 930 | # Process only one type | ||
| 931 | if (defined($type)) { | ||
| 932 | @ps = ( $type ); | ||
| 933 | } | ||
| 934 | |||
| 935 | my %diff = (); | ||
| 936 | for my $this (@ps) { | ||
| 937 | if (! $self->can($this)) { | ||
| 938 | $self->log->error("diff_current_state: state [$this] unknown, skipping"); | ||
| 939 | next; | ||
| 940 | } | ||
| 941 | my $ps = $self->$this; | ||
| 942 | if (! defined($ps)) { | ||
| 943 | $self->log->error("diff_current_state: failed [$this], skipping"); | ||
| 944 | next; | ||
| 945 | } | ||
| 946 | |||
| 947 | $self->log->info("diff_current_state: processing [$this] against [$this.csv]..."); | ||
| 948 | |||
| 949 | my $diff = $self->clean_ps_from_list($ps, "$this.csv", $sources); | ||
| 950 | if (! defined($ps)) { | ||
| 951 | $self->log->error("diff_current_state: failed clean_ps_from_list, skipping"); | ||
| 952 | next; | ||
| 953 | } | ||
| 954 | |||
| 955 | $diff{$this} = $diff; | ||
| 956 | } | ||
| 957 | |||
| 958 | # Regroup by similarity | ||
| 959 | my %grouped = (); | ||
| 960 | for my $k (keys %diff) { | ||
| 961 | my $list = $diff{$k}; | ||
| 962 | my $group_by = 'source'; | ||
| 963 | my $value = 'targets'; | ||
| 964 | for my $this (@$list) { | ||
| 965 | push @{$grouped{$k}{$this->{$group_by}}}, $this->{$value}; | ||
| 966 | } | ||
| 967 | } | ||
| 968 | |||
| 969 | return \%grouped; | ||
| 970 | } | ||
| 971 | |||
| 972 | 1; | ||
| 973 | |||
| 974 | __END__ | ||