1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
package audit
import (
"encoding/hex"
"fmt"
"net"
"regexp"
"strconv"
"strings"
)
var (
newEvent = false
netEvent = &Event{}
// RegExp for parse audit messages
// https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/security_guide/sec-understanding_audit_log_files
auditRE, _ = regexp.Compile(`([a-zA-Z0-9\-_]+)=([a-zA-Z0-9:'\-\/\"\.\,_\(\)]+)`)
rawEvent = make(map[string]string)
)
// amd64 syscalls definition
// if the platform is not amd64, it's redefined on Start()
var (
syscallSOCKET = "41"
syscallCONNECT = "42"
syscallSOCKETPAIR = "53"
syscallEXECVE = "59"
syscallSOCKETCALL = "102"
)
// /usr/include/x86_64-linux-gnu/bits/socket_type.h
const (
sockSTREAM = "1"
sockDGRAM = "2"
sockRAW = "3"
sockSEQPACKET = "5"
sockPACKET = "10"
// /usr/include/x86_64-linux-gnu/bits/socket.h
pfUNSPEC = "0"
pfLOCAL = "1" // PF_UNIX
pfINET = "2"
pfINET6 = "10"
// /etc/protocols
protoIP = "0"
protoTCP = "6"
protoUDP = "17"
)
// https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Security_Guide/sec-Audit_Record_Types.html
const (
AuditTypePROCTITLE = "type=PROCTITLE"
AuditTypeCWD = "type=CWD"
AuditTypePATH = "type=PATH"
AuditTypeEXECVE = "type=EXECVE"
AuditTypeSOCKADDR = "type=SOCKADDR"
AuditTypeSOCKETCALL = "type=SOCKETCALL"
AuditTypeEOE = "type=EOE"
)
var (
syscallSOCKETstr = fmt.Sprint("syscall=", syscallSOCKET)
syscallCONNECTstr = fmt.Sprint("syscall=", syscallCONNECT)
syscallSOCKETPAIRstr = fmt.Sprint("syscall=", syscallSOCKETPAIR)
syscallEXECVEstr = fmt.Sprint("syscall=", syscallEXECVE)
syscallSOCKETCALLstr = fmt.Sprint("syscall=", syscallSOCKETCALL)
)
// parseNetLine parses a SOCKADDR message type of the form:
// saddr string: inet6 host:2001:4860:4860::8888 serv:53
func parseNetLine(line string, decode bool) (family string, dstHost net.IP, dstPort int) {
// 0:4 - type
// 4:8 - port
// 8:16 - ip
switch family := line[0:4]; family {
// local
// case "0100":
// ipv4
case "0200":
octet2 := decodeString(line[4:8])
octet := decodeString(line[8:16])
host := fmt.Sprint(octet[0], ".", octet[1], ".", octet[2], ".", octet[3])
fmt.Printf("dest ip: %s -- %s:%s\n", line[4:8], octet2, host)
// ipv6
//case "0A00":
}
if decode == true {
line = decodeString(line)
}
pieces := strings.Split(line, " ")
family = pieces[0]
if family[:4] != "inet" {
return family, dstHost, 0
}
if len(pieces) > 1 && pieces[1][:5] == "host:" {
dstHost = net.ParseIP(strings.Split(pieces[1], "host:")[1])
}
if len(pieces) > 2 && pieces[2][:5] == "serv:" {
_dstPort, err := strconv.Atoi(strings.Split(line, "serv:")[1])
if err != nil {
dstPort = -1
} else {
dstPort = _dstPort
}
}
return family, dstHost, dstPort
}
// decodeString will try to decode a string encoded in hexadecimal.
// If the string can not be decoded, the original string will be returned.
// In that case, usually it means that it's a non-encoded string.
func decodeString(s string) string {
decoded, err := hex.DecodeString(s)
if err != nil {
return s
}
return fmt.Sprintf("%s", decoded)
}
// extractFields parsed an audit raw message, and extracts all the fields.
func extractFields(rawMessage string, newEvent *map[string]string) {
Lock.Lock()
defer Lock.Unlock()
if auditRE == nil {
newEvent = nil
return
}
fieldList := auditRE.FindAllStringSubmatch(rawMessage, -1)
if fieldList == nil {
newEvent = nil
return
}
for _, field := range fieldList {
(*newEvent)[field[1]] = field[2]
}
}
// populateEvent populates our Event from a raw parsed message.
func populateEvent(aevent *Event, eventFields *map[string]string) *Event {
if aevent == nil {
return nil
}
Lock.Lock()
defer Lock.Unlock()
for k, v := range *eventFields {
switch k {
//case "a0":
//case "a1":
//case "a2":
case "fam":
if v == "local" {
return nil
}
aevent.NetFamily = v
case "lport":
aevent.DstPort, _ = strconv.Atoi(v)
// TODO
/*case "addr":
fmt.Println("addr: ", v)
case "daddr":
fmt.Println("daddr: ", v)
case "laddr":
aevent.DstHost = net.ParseIP(v)
case "saddr":
parseNetLine(v, true)
fmt.Println("saddr:", v)
*/
case "exe":
aevent.ProcPath = strings.Trim(decodeString(v), "\"")
case "comm":
aevent.ProcName = strings.Trim(decodeString(v), "\"")
// proctitle may be truncated to 128 characters, so don't rely on it, parse /proc/<pid>/instead
//case "proctitle":
// aevent.ProcCmdLine = strings.Trim(decodeString(v), "\"")
case "tty":
aevent.TTY = v
case "pid":
aevent.Pid, _ = strconv.Atoi(v)
case "ppid":
aevent.PPid, _ = strconv.Atoi(v)
case "uid":
aevent.UID, _ = strconv.Atoi(v)
case "gid":
aevent.Gid, _ = strconv.Atoi(v)
case "success":
aevent.Success = v
case "cwd":
aevent.ProcDir = strings.Trim(decodeString(v), "\"")
case "inode":
aevent.INode, _ = strconv.Atoi(v)
case "dev":
aevent.Dev = v
case "mode":
aevent.ProcMode = v
case "ouid":
aevent.OUid, _ = strconv.Atoi(v)
case "ogid":
aevent.OGid, _ = strconv.Atoi(v)
case "syscall":
aevent.Syscall, _ = strconv.Atoi(v)
case "exit":
aevent.Exit, _ = strconv.Atoi(v)
case "type":
aevent.EventType = v
case "msg":
parts := strings.Split(v[6:], ":")
aevent.Timestamp = parts[0]
aevent.Serial = parts[1][:len(parts[1])-1]
}
}
return aevent
}
// parseEvent parses an auditd event, discards the unwanted ones, and adds
// the ones we're interested in to an array.
// We're only interested in the socket,socketpair,connect and execve syscalls.
// Events from us are excluded.
//
// When we received an event, we parse and add it to the list as soon as we can.
// If the next messages of the set have additional information, we update the
// event.
func parseEvent(rawMessage string, eventChan chan<- Event) {
if newEvent == false && strings.Index(rawMessage, OpensnitchRulesKey) == -1 {
return
}
aEvent := make(map[string]string)
if strings.Index(rawMessage, syscallSOCKETstr) != -1 ||
strings.Index(rawMessage, syscallCONNECTstr) != -1 ||
strings.Index(rawMessage, syscallSOCKETPAIRstr) != -1 ||
strings.Index(rawMessage, syscallEXECVEstr) != -1 ||
strings.Index(rawMessage, syscallSOCKETCALLstr) != -1 {
extractFields(rawMessage, &aEvent)
if aEvent == nil {
return
}
newEvent = true
netEvent = &Event{}
netEvent = populateEvent(netEvent, &aEvent)
AddEvent(netEvent)
} else if newEvent == true && strings.Index(rawMessage, AuditTypePROCTITLE) != -1 {
extractFields(rawMessage, &aEvent)
if aEvent == nil {
return
}
netEvent = populateEvent(netEvent, &aEvent)
AddEvent(netEvent)
} else if newEvent == true && strings.Index(rawMessage, AuditTypeCWD) != -1 {
extractFields(rawMessage, &aEvent)
if aEvent == nil {
return
}
netEvent = populateEvent(netEvent, &aEvent)
AddEvent(netEvent)
} else if newEvent == true && strings.Index(rawMessage, AuditTypeEXECVE) != -1 {
extractFields(rawMessage, &aEvent)
if aEvent == nil {
return
}
netEvent = populateEvent(netEvent, &aEvent)
AddEvent(netEvent)
} else if newEvent == true && strings.Index(rawMessage, AuditTypePATH) != -1 {
extractFields(rawMessage, &aEvent)
if aEvent == nil {
return
}
netEvent = populateEvent(netEvent, &aEvent)
AddEvent(netEvent)
} else if newEvent == true && strings.Index(rawMessage, AuditTypeSOCKADDR) != -1 {
extractFields(rawMessage, &aEvent)
if aEvent == nil {
return
}
netEvent = populateEvent(netEvent, &aEvent)
AddEvent(netEvent)
if EventChan != nil {
eventChan <- *netEvent
}
} else if newEvent == true && strings.Index(rawMessage, AuditTypeEOE) != -1 {
newEvent = false
AddEvent(netEvent)
if EventChan != nil {
eventChan <- *netEvent
}
}
}
|