audit_log_parser-aeber 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fe0f36d6ca6ccea8f032db5e88ffb6abdfc6a8149447841772fc211ddde4ea0e
4
+ data.tar.gz: db3ebea77d0385b919bea09af2d26ada8641b884b183601864869bceb87f28f9
5
+ SHA512:
6
+ metadata.gz: bf8f14ca1bcab9f93de66c072bb7dad01dafbe1088054183f52144203866252f89f951f81622953db292faa629c07f2bba1d6c2c1bc3899246e0b16c6ed9e51c
7
+ data.tar.gz: 3f03a2c2cb6aaaf27efe1e07612b939e4238b055ea8fbb5699e45109817b770dd29b26df545db3ccd577a2e4663baa01f55185beb0d90fb3b6b7f75e85062203
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+ test.rb
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.3.8
7
+ - 2.4.5
8
+ - 2.5.3
9
+ - 2.6.0-preview2
10
+ before_install: gem install bundler -v 1.16.6
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in audit_log_parser.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 winebarrel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # audit_log_parser
2
+
3
+ It is a library for parsing [linux's audit log](https://github.com/linux-audit/audit-documentation/wiki).
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/audit_log_parser.svg)](http://badge.fury.io/rb/audit_log_parser)
6
+ [![Build Status](https://travis-ci.org/winebarrel/audit_log_parser.svg?branch=master)](https://travis-ci.org/winebarrel/audit_log_parser)
7
+ [![](https://img.shields.io/badge/rubydoc-reference-blue.svg)](https://www.rubydoc.info/gems/audit_log_parser)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'audit_log_parser'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install audit_log_parser
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ #!/usr/bin/env ruby
29
+ require 'audit_log_parser'
30
+ require 'pp'
31
+
32
+ audit_log1 = <<EOS
33
+ type=SYSCALL msg=audit(1364481363.243:24287): arch=c000003e syscall=2 success=no exit=-13 a0=7fffd19c5592 a1=0 a2=7fffd19c4b50 a3=a items=1 ppid=2686 pid=3538 auid=500 uid=500 gid=500 euid=500 suid=500 fsuid=500 egid=500 sgid=500 fsgid=500 tty=pts0 ses=1 comm="cat" exe="/bin/cat" subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key="sshd_config"
34
+ EOS
35
+
36
+ pp AuditLogParser.parse_line(audit_log1)
37
+ #=> {"header"=>{"type"=>"SYSCALL", "msg"=>"audit(1364481363.243:24287)"},
38
+ # "body"=>
39
+ # {"arch"=>"c000003e",
40
+ # "syscall"=>"2",
41
+ # "success"=>"no",
42
+ # "exit"=>"-13",
43
+ # "a0"=>"7fffd19c5592",
44
+ # "a1"=>"0",
45
+ # "a2"=>"7fffd19c4b50",
46
+ # "a3"=>"a",
47
+ # "items"=>"1",
48
+ # "ppid"=>"2686",
49
+ # "pid"=>"3538",
50
+ # "auid"=>"500",
51
+ # "uid"=>"500",
52
+ # "gid"=>"500",
53
+ # "euid"=>"500",
54
+ # "suid"=>"500",
55
+ # "fsuid"=>"500",
56
+ # "egid"=>"500",
57
+ # "sgid"=>"500",
58
+ # "fsgid"=>"500",
59
+ # "tty"=>"pts0",
60
+ # "ses"=>"1",
61
+ # "comm"=>"\"cat\"",
62
+ # "exe"=>"\"/bin/cat\"",
63
+ # "subj"=>"unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023",
64
+ # "key"=>"\"sshd_config\""}}
65
+
66
+ audit_log2 = <<EOS
67
+ type=USER_AUTH msg=audit(1364475353.159:24270): user pid=3280 uid=500 auid=500 ses=1 subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 msg='op=PAM:authentication acct="root" exe="/bin/su" hostname=? addr=? terminal=pts/0 res=failed'
68
+ EOS
69
+
70
+ pp AuditLogParser.parse_line(audit_log2)
71
+ #=> {"header"=>{"type"=>"USER_AUTH", "msg"=>"audit(1364475353.159:24270)"},
72
+ # "body"=>
73
+ # {"user pid"=>"3280",
74
+ # "uid"=>"500",
75
+ # "auid"=>"500",
76
+ # "ses"=>"1",
77
+ # "subj"=>"unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023",
78
+ # "msg"=>
79
+ # {"op"=>"PAM:authentication",
80
+ # "acct"=>"\"root\"",
81
+ # "exe"=>"\"/bin/su\"",
82
+ # "hostname"=>"?",
83
+ # "addr"=>"?",
84
+ # "terminal"=>"pts/0",
85
+ # "res"=>"failed"}}}
86
+
87
+ audit_log3 = <<EOS
88
+ type=PATH msg=audit(1364481363.243:24287): item=0 name="/etc/ssh/sshd_config" inode=409248 dev=fd:00 mode=0100600 ouid=0 ogid=0 rdev=00:00 obj=system_u:object_r:etc_t:s0
89
+ EOS
90
+
91
+ pp AuditLogParser.parse_line(audit_log3, flatten: true)
92
+ #=> {"header_type"=>"PATH",
93
+ # "header_msg"=>"audit(1364481363.243:24287)",
94
+ # "body_item"=>"0",
95
+ # "body_name"=>"\"/etc/ssh/sshd_config\"",
96
+ # "body_inode"=>"409248",
97
+ # "body_dev"=>"fd:00",
98
+ # "body_mode"=>"0100600",
99
+ # "body_ouid"=>"0",
100
+ # "body_ogid"=>"0",
101
+ # "body_rdev"=>"00:00",
102
+ # "body_obj"=>"system_u:object_r:etc_t:s0"}
103
+ ```
104
+
105
+ ## Related Links
106
+
107
+ * [7.6. Understanding Audit Log Files - Red Hat Customer Portal](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/security_guide/sec-understanding_audit_log_files)
108
+ * [SPEC Writing Good Events · linux-audit/audit-documentation Wiki](https://github.com/linux-audit/audit-documentation/wiki/SPEC-Writing-Good-Events)
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'audit_log_parser/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'audit_log_parser-aeber'
8
+ spec.version = AuditLogParser::VERSION
9
+ spec.authors = ['winebarrel', 'aeber']
10
+ spec.email = ['']
11
+
12
+ spec.summary = %q{It is a library for parsing.}
13
+ spec.description = %q{It is a library for parsing.}
14
+ spec.homepage = 'https://github.com/aeber/audit_log_parser'
15
+ spec.license = 'MIT'
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_development_dependency 'bundler'
27
+ spec.add_development_dependency 'rake'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "audit_log_parser"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ class AuditLogParser
2
+ VERSION = '0.1.4'
3
+ end
@@ -0,0 +1,106 @@
1
+ require 'strscan'
2
+ require 'audit_log_parser/version'
3
+
4
+ class AuditLogParser
5
+ class Error < StandardError; end
6
+
7
+ def self.parse(src, flatten: false)
8
+ src.each_line.map do |line|
9
+ parse_line(line, flatten: flatten)
10
+ end
11
+ end
12
+
13
+ def self.parse_line(line, flatten: false)
14
+ line = line.strip
15
+
16
+ if line !~ /type=\w+ msg=audit\([\d.:]+\): */
17
+ raise Error, "Invalid audit log header: #{line.inspect}"
18
+ end
19
+
20
+ header, body = line.split(/\): */, 2)
21
+ header << ')'
22
+ header.sub!(/: *\z/, '')
23
+ header = parse_header(header)
24
+ unless body.empty?
25
+ body, enriched = body.split('\u001D', 2)
26
+ end
27
+ body = parse_body(body)
28
+ if enriched.nil?
29
+ result = {'header' => header, 'body' => body}
30
+ else
31
+ result = {'header' => header, 'body' => body, 'enriched'=>enriched.strip}
32
+ end
33
+ flatten ? flatten_hash(result) : result
34
+ end
35
+
36
+ def self.parse_header(header)
37
+ result = {}
38
+
39
+ header.split(' ').each do |kv|
40
+ key, value = kv.split('=', 2)
41
+ result[key] = value
42
+ end
43
+
44
+ result
45
+ end
46
+ private_class_method :parse_header
47
+
48
+ def self.parse_body(body)
49
+ if body.empty?
50
+ return {}
51
+ elsif !body.include?('=')
52
+ raise Error, "Invalid audit log body: #{body.inspect}"
53
+ end
54
+
55
+ result = {}
56
+ ss = StringScanner.new(body)
57
+
58
+ while key = ss.scan_until(/=/)
59
+ if key.include?(', ')
60
+ msg, key = key.split(', ', 2)
61
+ result['_message'] = msg.strip
62
+ end
63
+
64
+ key.chomp!('=').strip!
65
+ value = ss.getch
66
+
67
+ case value
68
+ when nil
69
+ break
70
+ when ' '
71
+ next
72
+ when '"'
73
+ value << ss.scan_until(/"/)
74
+ when "'"
75
+ nest = ss.scan_until(/'/)
76
+ nest.chomp!("'")
77
+ value = parse_body(nest)
78
+ else
79
+ value << ss.scan_until(/( |\z)/)
80
+ value.chomp!(' ')
81
+ end
82
+
83
+ result[key] = value
84
+ end
85
+
86
+ unless ss.rest.empty?
87
+ raise "must not happen: #{body}"
88
+ end
89
+
90
+ result
91
+ end
92
+ private_class_method :parse_body
93
+
94
+ def self.flatten_hash(h)
95
+ h.flat_map {|key, value|
96
+ if value.is_a?(Hash)
97
+ flatten_hash(value).map do |sub_key, sub_value|
98
+ ["#{key}_#{sub_key}", sub_value]
99
+ end
100
+ else
101
+ [[key, value]]
102
+ end
103
+ }.to_h
104
+ end
105
+ private_class_method :flatten_hash
106
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: audit_log_parser-aeber
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - winebarrel
8
+ - aeber
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2023-10-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ description: It is a library for parsing.
57
+ email:
58
+ - ''
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - audit_log_parser.gemspec
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/audit_log_parser.rb
74
+ - lib/audit_log_parser/version.rb
75
+ homepage: https://github.com/aeber/audit_log_parser
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubygems_version: 3.3.25
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: It is a library for parsing.
98
+ test_files: []