Log.pm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. use File::Which();
  7. use UUID::Tiny();
  8. use List::Util qw{uniq};
  9. # ABSTRACT: auditd log parser with minimal dependencies, using no perl features past 5.12
  10. =head1 WHY
  11. I had to do reporting for non-incremental backups.
  12. I needed something faster than GNU find, and which took less memory as well.
  13. I didn't want to stat 1M+ files.
  14. Just reads a log and keeps the bare minimum useful information.
  15. You can use auditd for a number of other interesting purposes, which this should support as well.
  16. =head1 SYNOPSIS
  17. my $parser = Audit::Log->new();
  18. my $rows = $parser->search(
  19. type => qr/path/i,
  20. nametype => qr/delete|create|normal/i,
  21. name => qr/somefile.txt/i,
  22. );
  23. =head1 CONSTRUCTOR
  24. =head2 new(STRING path, ARRAY returning) = Audit::Log
  25. Opens the provided audit log path when searching, or
  26. /var/log/audit/audit.log
  27. if none is provided.
  28. Also can filter returned keys by the provided array to not allocate unnecesarily in low mem situations.
  29. =head3 using with ausearch
  30. It's common to have the audit log be quite verbose, and log-rotated.
  31. To get around that you can dump pieces of the audit log as appropriate with ausearch.
  32. Here's an example of dumping keyed events for the last day, which you could then load into new().
  33. ausearch --raw --key backupwatch -ts `date --date yesterday '+%x'` > yesterdays-audit.log
  34. If you pass 'ausearch' as the audit log path to new(), we will pipe-open to this in subsequent search() calls.
  35. =head3 configuring retention
  36. The audit log is quite likely to have very limited retention.
  37. This is configured in the max_log_file and num_logs parameter of /etc/auditd/audit.conf
  38. You will only have max_log_file * num_logs MB of events stored, so plan according to how much you need to watch.
  39. Your specific use case should be observed, and tuned accordingly.
  40. For example, the average audit log line is ~200 bytes, so you can get maybe 40k entries per log at max_log_file=8.
  41. Each file action is (worst case) 5 lines in the log, resulting in maybe 8k file modifications per 8MB logfile.
  42. As such, stashing results or watching very tightly around blocks of functionality is highly recommended.
  43. Especially in situations such as public servers which are likely to get a large amount of SSH bounces recorded in the log by default.
  44. The block-scoped methods in this module are built to serve precisely this use case.
  45. =cut
  46. sub new {
  47. my ($class, $path, @returning) = @_;
  48. $path = '/var/log/audit/audit.log' unless $path;
  49. my $fullpath = File::Which::which('ausearch');
  50. if ($path eq 'ausearch') {
  51. die "Cannot find ausearch" unless -f $fullpath;
  52. } else {
  53. die "Cannot access $path" unless -f $path;
  54. }
  55. return bless({ path => $path, ausearch => $fullpath, returning => \@returning}, $class);
  56. }
  57. =head1 METHODS
  58. =head2 search(key => constraint) = ARRAY[HashRef{}]
  59. Searches the log for lines where the value corresponding to the provided key matches the constraint, which is expected to be a quoted regex.
  60. If no constraints are provided, all matching rows will be returned.
  61. Example:
  62. my $rows = $parser->search( type => qr/path/i, nametype=qr/delete|create|normal/i );
  63. The above effectively will get you a list of all file modifications/creations/deletions in watched directories.
  64. Adds in a 'line' parameter to rows returned in case you want to know which line in the log it's on.
  65. Also adds a 'timestamp' parameter, since this is a parsed parameter.
  66. =head3 Speeding it up: by event
  67. Auditd logs are also structured in blocks separated between SYSCALL lines, which are normally filtered by 'key', which corresponds to rule name.
  68. We can speed up processing by ignoring events of the incorrect key.
  69. Example:
  70. my $rows = $parser->search( type => qr/path/i, nametype=qr/delete|create|normal/i, key => qr/backup_watch/i );
  71. The above will ignore events from all rules save those from the "backup_watch" rule.
  72. =head3 Speeding it up: by timeframe
  73. Auditd log rules also print a timestamp, which means we need a numeric comparison.
  74. Pass in 'older' and 'newer', and we can filter out things appropriately.
  75. Example:
  76. # Get all records that are from the last 24 hours
  77. my $rows = $parser->search( type => qr/path/i, nametype=qr/delete|create|normal/i, newer => ( time - 86400 ) );
  78. =head3 Getting full paths with CWDs
  79. PATH records don't actually store the full path to what is acted upon unless the process acting upon it used an absolute path.
  80. Thankfully, SYSCALL records are are always followed by a CWD record. As such we add the 'cwd' field to all subsequent records.
  81. As such, you can build full paths like so:
  82. my $parser = Audit::Log->new(undef, 'name', 'cwd');
  83. my $rows = $parser->search( type => qr/path/i, nametype=qr/delete|create|normal/i );
  84. my @full_paths = map { "$_->{cwd}/$_->{name}" } @$rows;
  85. =head3 Filtering by command
  86. SYSCALL records store the command which executed the call. This is exposed as part of the parse for each child record, such as PATH or DAEMON_* records.
  87. Example of getting all the commands run which triggered audit events:
  88. my $parser = Audit::Log->new(undef, 'exe')
  89. my $rows = $parser->search();
  90. =cut
  91. sub search {
  92. my ($self,%options) = @_;
  93. my $ret = [];
  94. my $in_block = 1;
  95. my $line = -1;
  96. my ($cwd, $exe, $comm) = ('','','');
  97. my $fh;
  98. if ($self->{path} eq 'ausearch') {
  99. # TODO support --comm, -ts, -sv
  100. my @args = qw{--input-logs --raw};
  101. push(@args, ('-k', $self->{key}));
  102. open($fh, '|', qq|$self->{fullpath} @args|) or die "Could not run $self->{fullpath}!";
  103. } else {
  104. open($fh, '<', $self->{path}) or die "Could not open $self->{path}!";
  105. }
  106. LINE: while (<$fh>) {
  107. next if index( $_, 'SYSCALL') < 0 && !$in_block;
  108. # I am trying to cheat here to snag the timestamp.
  109. my $msg_start = index($_, 'msg=audit(') + 10;
  110. my $msg_end = index($_, ':');
  111. my $timestamp = substr($_, $msg_start, $msg_end - $msg_start);
  112. next if $options{older} && $timestamp > $options{older};
  113. next if $options{newer} && $timestamp < $options{newer};
  114. # Snag CWDs
  115. if ( index( $_, 'type=CWD') == 0) {
  116. my $cwd_start = index($_, 'cwd="') + 5;
  117. my $cwd_end = index($_, "\n") - 1;
  118. $cwd = substr($_, $cwd_start, $cwd_end - $cwd_start);
  119. $line++;
  120. next;
  121. }
  122. # Replace GROUP SEPARATOR usage with simple spaces
  123. s/[\x1D]/ /g;
  124. my %parsed = map {
  125. my @out = split(/=/, $_);
  126. shift @out, join('=',@out)
  127. } grep { $_ } map {
  128. my $subj = $_;
  129. $subj =~ s/"//g;
  130. chomp $subj;
  131. $subj
  132. } split(/ /,$_);
  133. $line++;
  134. $parsed{line} = $line;
  135. $parsed{timestamp} = $timestamp;
  136. $parsed{cwd} = $cwd;
  137. $parsed{exe} //= $exe;
  138. $parsed{comm} //= $comm;
  139. if (exists $options{key} && $parsed{type} eq 'SYSCALL') {
  140. $in_block = $parsed{key} =~ $options{key};
  141. $exe = $parsed{exe};
  142. $comm = $parsed{comm};
  143. $cwd = '';
  144. next unless $in_block;
  145. }
  146. # Check constraints BEFORE filtering returned values, this is a WHERE clause
  147. CONSTRAINT: foreach my $constraint (keys(%options)) {
  148. next CONSTRAINT if !exists $parsed{$constraint};
  149. next LINE if $parsed{$constraint} !~ $options{$constraint};
  150. }
  151. # Filter fields for RETURNING clause
  152. if (@{$self->{returning}}) {
  153. foreach my $field (keys(%parsed)) {
  154. delete $parsed{$field} unless grep { $field eq $_ } @{$self->{returning}};
  155. }
  156. }
  157. push(@$ret,\%parsed);
  158. }
  159. close($fh);
  160. return $ret;
  161. }
  162. =head1 FUNCTIONS
  163. All these are block-scoped watchers provided for convenience and testing purposes.
  164. =head2 file_changes(CODE block, LIST dirs) = ARRAY
  165. Returns the list of files that changed in the proceeding block.
  166. my @changes = file_changes { ... } qw{/tmp /mydir};
  167. is(scalar(@changes), 0, "No spooky action at a distance");
  168. =cut
  169. sub file_changes(&@) {
  170. my ($block,@dirs) = @_;
  171. my %rules;
  172. # Instruct auditctl to add UUID based rules
  173. foreach my $dir (@dirs) {
  174. $rules{$dir} = UUID::Tiny::create_uuid_as_string(UUID::Tiny::UUID_V1, UUID::Tiny::UUID_NS_DNS);
  175. #TODO handle errors, etc
  176. system(qw[auditctl -w], $dir, qw[-p rw -k], $rules{$dir});
  177. }
  178. $block->();
  179. # Unload the rule, flush the log
  180. foreach my $dir (@dirs) {
  181. #TODO errors, flush
  182. system(qw[auditctl -W], $dir);
  183. }
  184. # Grab events
  185. my $parser = Audit::Log->new('ausearch', qw{name cwd});
  186. # TODO support arrayref
  187. my $entries = $parser->search('key' => [values(%rules)]);
  188. return uniq map { $_->{name} =~ m/^\// ? $_->{name} : "$_->{cwd}/$_->{name}" } @$entries;
  189. }
  190. 1;