Log.pm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package Audit::Log;
  2. use strict;
  3. use warnings;
  4. use 5.006;
  5. use v5.12.0; # Before 5.006, v5.10.0 would not be understood.
  6. # ABSTRACT: auditd log parser with no external dependencies, using no perl features past 5.12
  7. =head1 WHY
  8. I had to do reporting for non-incremental backups.
  9. I needed something faster than GNU find, and which took less memory as well.
  10. I didn't want to stat 1M+ files.
  11. Just reads a log and keeps the bare minimum useful information.
  12. You can use auditd for a number of other interesting purposes, which this should support as well.
  13. =head1 SYNOPSIS
  14. my $parser = Audit::Log->new();
  15. my $rows = $parser->search(
  16. type => qr/path/i,
  17. nametype => qr/delete|create/i,
  18. );
  19. =head1 CONSTRUCTOR
  20. =head2 new(STRING path, ARRAY returning) = Audit::Log
  21. Opens the provided audit log path when searching, or
  22. /var/log/audit/audit.log
  23. if none is provided.
  24. Also can filter returned keys by the provided array to not allocate unnecesarily in low mem situations.
  25. =cut
  26. sub new {
  27. my ($class, $path, @returning) = @_;
  28. $path = '/var/log/audit/audit.log' unless $path;
  29. die "Cannot access $path" unless -f $path;
  30. return bless({ path => $path, returning => \@returning}, $class);
  31. }
  32. =head1 METHODS
  33. =head2 search(key => constraint) = ARRAY[HashRef{}]
  34. Searches the log for lines where the value corresponding to the provided key matches the constraint, which is expected to be a quoted regex.
  35. If no constraints are provided, all matching rows will be returned.
  36. Example:
  37. my $rows = $parser->search( type => qr/path/i, nametype=qr/delete|create/i );
  38. The above effectively will get you a list of all file modifications/creations/deletions in watched directories.
  39. Adds in a 'line' parameter to rows returned in case you want to know which line in the log it's on.
  40. Also adds a 'timestamp' parameter, since this is a parsed parameter.
  41. =head3 Speeding it up: by event
  42. Auditd logs are also structured in blocks separated between SYSCALL lines, which are normally filtered by 'key', which corresponds to rule name.
  43. We can speed up processing by ignoring events of the incorrect key.
  44. Example:
  45. my $rows = $parser->search( type => qr/path/i, nametype=qr/delete|create/i, key => qr/backup_watch/i );
  46. The above will ignore events from all rules save those from the "backup_watch" rule.
  47. =head3 Speeding it up: by timeframe
  48. Auditd log rules also print a timestamp, which means we need a numeric comparison.
  49. Pass in 'older' and 'newer', and we can filter out things appropriately.
  50. Example:
  51. # Get all records that are from the last 24 hours
  52. my $rows = $parser->search( type => qr/path/i, nametype=qr/delete|create/i, newer => ( time - 86400 ) );
  53. Handling rotated logs is left as an exercise for the reader.
  54. =cut
  55. sub search {
  56. my ($self,%options) = @_;
  57. my $ret = [];
  58. my $in_block = 1;
  59. my $line = -1;
  60. open(my $fh, '<', $self->{path});
  61. LINE: while (<$fh>) {
  62. next if index( $_, 'SYSCALL') < 0 && !$in_block;
  63. # I am trying to cheat here to snag the timestamp.
  64. my $msg_start = index($_, 'msg=audit(') + 10;
  65. my $msg_end = index($_, ':');
  66. my $timestamp = substr($_, $msg_start, $msg_end - $msg_start)."\n";
  67. next if $options{older} && $timestamp > $options{older};
  68. next if $options{newer} && $timestamp < $options{newer};
  69. # Replace GROUP SEPARATOR usage with simple spaces
  70. s/[\x1D]/ /g;
  71. my %parsed = map {
  72. my @out = split(/=/, $_);
  73. shift @out, join('=',@out)
  74. } grep { $_ } map {
  75. my $subj = $_;
  76. $subj =~ s/"//g;
  77. chomp $subj;
  78. $subj
  79. } split(/ /,$_);
  80. $line++;
  81. $parsed{line} = $line;
  82. chomp $timestamp;
  83. $parsed{timestamp} = $timestamp;
  84. if (exists $options{key} && $parsed{type} eq 'SYSCALL') {
  85. $in_block = $parsed{key} =~ $options{key};
  86. next unless $in_block;
  87. }
  88. # Check constraints BEFORE filtering returned values, this is a WHERE clause
  89. CONSTRAINT: foreach my $constraint (keys(%options)) {
  90. next CONSTRAINT if !exists $parsed{$constraint};
  91. next LINE if $parsed{$constraint} !~ $options{$constraint};
  92. }
  93. # Filter fields for RETURNING clause
  94. if (@{$self->{returning}}) {
  95. foreach my $field (keys(%parsed)) {
  96. delete $parsed{$field} unless grep { $field eq $_ } @{$self->{returning}};
  97. }
  98. }
  99. push(@$ret,\%parsed);
  100. }
  101. close($fh);
  102. return $ret;
  103. }
  104. 1;