Log.pm 4.3 KB

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