go package for interfacing with Linux audit
Перейти к файлу
Aaron Meihm 6f76c4a779
Merge pull request #40 from lzakharov/fix-rules-check-path
Fix rules checkPath error printing
2019-04-22 09:58:41 -05:00
auditprint add ErrorAuditParse which describes parser errors 2017-08-28 15:31:23 -05:00
headers Add reverse Mapping for syscall values => names to avoid map assigns 2016-08-16 15:16:44 +05:30
testdata make strict_path_check rule specific, fix bug in option handling 2017-08-25 16:20:13 -05:00
vendor/github.com/lunixbochs/struc remove vendored errors package 2017-08-25 16:41:55 -05:00
.gitignore gitignore 2017-03-02 09:34:22 -06:00
.travis.yml use service 2017-08-25 11:28:43 -05:00
CODE_OF_CONDUCT.md Add Mozilla Code of Conduct file 2019-03-29 09:52:34 -07:00
LICENSE Initial commit 2014-11-06 15:37:37 -05:00
Makefile add a profile makefile target 2017-08-24 16:08:28 -05:00
README.md add example of using auditprint to README 2017-08-28 16:21:33 -05:00
audit_constant.go add a note regarding constant generation 2017-08-24 13:38:02 -05:00
audit_events.go support partial record reassembly and handle EOE 2017-08-28 16:09:51 -05:00
auditconstant_string.go update generated auditconstant_string.go 2017-08-23 09:37:45 -05:00
buffer.go support partial record reassembly and handle EOE 2017-08-28 16:09:51 -05:00
interpret.go use stdlib errors package instead of externally referenced 2017-08-25 16:41:55 -05:00
libaudit.go handle cases of partial read from kernel netlink producer 2017-08-28 15:31:28 -05:00
libaudit_test.go add license header to source, remove example from package comments 2017-08-23 09:36:27 -05:00
lookup_tables.go don't export MsgTypeTab 2017-08-24 12:52:50 -05:00
parser.go add ErrorAuditParse which describes parser errors 2017-08-28 15:31:23 -05:00
parser_test.go add ErrorAuditParse which describes parser errors 2017-08-28 15:31:23 -05:00
rules.go Fix rules checkPath error printing 2019-04-19 20:32:55 +03:00
rules_test.go make strict_path_check rule specific, fix bug in option handling 2017-08-25 16:20:13 -05:00
s2i_type_conversion.json removed autid-go components 2015-11-20 02:54:28 +05:30

README.md

libaudit in Go

libaudit-go is a go package for interfacing with Linux audit.

Build Status Go Report Card

libaudit-go is a pure Go client library for interfacing with the Linux auditing framework. It provides functions to interact with the auditing subsystems over Netlink, including controlling the rule set and obtaining/interpreting incoming audit events.

libaudit-go can be used to build go applications which perform tasks similar to the standard Linux auditing daemon auditd.

To get started see package documentation at godoc.

For a simple example of usage, see the auditprint tool included in this repository.

sudo service stop auditd
go get -u github.com/mozilla/libaudit-go
cd $GOPATH/src/github.com/mozilla/libaudit-go
go install github.com/mozilla/libaudit-go/auditprint
sudo $GOPATH/bin/auditprint testdata/rules.json

Some key functions are discussed in the overview section below.

Overview

General

NewNetlinkConnection

To use libaudit-go programs will need to initialize a new Netlink connection. NewNetlinkConnection can be used to allocate a new NetlinkConnection type which can then be passed to other functions in the library.

s, err := libaudit.NewNetlinkConnection()
if err != nil {
        fmt.Printf("NewNetlinkConnection: %v\n", err)
} 
defer s.Close()

NetlinkConnection provides a Send and Receive method to send and receive Netlink messages to the kernel, however generally applications will use the various other functions included in libaudit-go and do not need to call these functions directly.

GetAuditEvents

GetAuditEvents starts an audit event monitor in a go-routine and returns. Programs can call this function and specify a callback function as an argument. When the audit event monitor receives a new event, this callback function will be called with the parsed AuditEvent as an argument.


func myCallback(msg *libaudit.AuditEvent, err error) {
        if err != nil {
            // An error occurred getting or parsing the audit event
            return
        }
	// Print the fields
        fmt.Println(msg.Data)
	// Print the raw event
        fmt.Println(msg.Raw)
}

libaudit.GetAuditEvents(s, myCallback)
GetRawAuditEvents

GetRawAuditEvents behaves in a similar manner to GetAuditEvents, however programs can use this function to instead just retrieve raw audit events from the kernel as a string, instead of having libaudit-go parse these audit events into an AuditEvent type.

Audit Rules

Audit rules can be loaded into the kernel using libaudit-go, however the format differs from the common rule set used by userspace tools such as auditctl/auditd.

libaudit-go rulesets are defined as a JSON document. See rules.json as an example. The libaudit-go type which stores the rule set is AuditRules.

SetRules

SetRules can be used to load an audit rule set into the kernel. The function takes a marshalled AuditRules type as an argument (slice of bytes), and converts the JSON based rule set into a set of audit rules suitable for submission to the kernel.

The function then makes the required Netlink calls to clear the existing rule set and load the new rules.

// Load all rules from a file
content, err := ioutil.ReadFile("audit.rules.json")
if err != nil {
        fmt.Printf("error: %v\n", err)
	os.Exit(1)
}

// Set audit rules
err = libaudit.SetRules(s, content)
if err != nil {
        fmt.Printf("error: %v\n", err)
        os.Exit(1)
}