This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
interpret.go
1491 lines (1388 loc) · 37.3 KB
/
interpret.go
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package libaudit
import (
"bytes"
"encoding/hex"
"fmt"
"net"
"os/user"
"runtime"
"strconv"
"strings"
"syscall"
"github.com/lunixbochs/struc"
"github.com/mozilla/libaudit-go/headers"
)
// fieldType denotes the integer values of various fields occuring in audit messages
type fieldType int
const (
typeUID fieldType = iota
typeGID
typeSyscall
typeArch
typeExit
typePerm
typeEscaped
typeMode
typeSockaddr
typePromisc
typeCapability
typeSuccess
typeA0
typeA1
typeA2
typeA3
typeSignal
typeList
typeTTYData
typeSession
typeCapBitmap
typeNFProto
typeICMP
typeProtocol
typeAddr
typePersonality
typeOFlag
typeSeccomp
typeMmap
typeMacLabel
typeProctile
typeUnclassified
typeModeShort
)
// interpretField takes fieldName and the encoded fieldValue (part of the audit message) and
// returns the string representations for the values e.g., syscall numbers to names, uids to
// usernames, etc.
func interpretField(fieldName string, fieldValue string, msgType auditConstant, r record) (string, error) {
// the code follows the same logic as these auditd functions (call chain is shown) =>
// auparse_interpret_field() [auparse.c] -> nvlist_interp_cur_val(const rnode *r) [nvlist.c]
// -> interpret(r) [interpret.c] -> type = auparse_interp_adjust_type(r->type, id.name, id.val); [interpret.c]
// out = auparse_do_interpretation(type, &id); [interpret.c]
var (
ftype fieldType
result string
err error
ok bool
)
if msgType == AUDIT_EXECVE && strings.HasPrefix(fieldName, "a") &&
fieldName != "argc" && strings.Index(fieldName, "_len") == -1 {
ftype = typeEscaped
} else if msgType == AUDIT_AVC && fieldName == "saddr" {
ftype = typeUnclassified
} else if msgType == AUDIT_USER_TTY && fieldName == "msg" {
ftype = typeEscaped
} else if msgType == AUDIT_NETFILTER_PKT && fieldName == "saddr" {
ftype = typeAddr
} else if fieldName == "acct" {
if strings.HasPrefix(fieldValue, `"`) {
ftype = typeEscaped
} else if _, err := strconv.ParseInt(fieldValue, 16, -1); err != nil {
ftype = typeEscaped
} else {
ftype = typeUnclassified
}
} else if msgType == AUDIT_MQ_OPEN && fieldName == "mode" {
ftype = typeModeShort
} else if msgType == AUDIT_CRYPTO_KEY_USER && fieldName == "fp" {
ftype = typeUnclassified
} else if fieldName == "id" && (msgType == AUDIT_ADD_GROUP || msgType == AUDIT_GRP_MGMT ||
msgType == AUDIT_DEL_GROUP) {
ftype = typeGID
} else {
if ftype, ok = fieldLookupMap[fieldName]; !ok {
ftype = typeUnclassified
}
}
switch ftype {
case typeUID:
result, err = printUID(fieldValue)
if err != nil {
return "", err
}
case typeGID:
// XXX printGID is currently only a stub
result, err = printGID(fieldValue)
if err != nil {
return "", err
}
case typeSyscall:
result, err = printSyscall(fieldValue)
if err != nil {
return "", err
}
case typeArch:
return printArch()
case typeExit:
result, err = printExit(fieldValue) // peek on exit codes (stderror)
if err != nil {
return "", err
}
case typePerm:
result, err = printPerm(fieldValue)
if err != nil {
return "", err
}
case typeEscaped:
result, err = printEscaped(fieldValue)
if err != nil {
return "", err
}
case typeMode:
result, err = printMode(fieldValue, 8)
if err != nil {
return "", err
}
case typeModeShort:
result, err = printModeShort(fieldValue, 8)
if err != nil {
return "", err
}
case typeSockaddr:
result, err = printSockAddr(fieldValue)
if err != nil {
return "", err
}
case typePromisc:
result, err = printPromiscuous(fieldValue)
if err != nil {
return "", err
}
case typeCapability:
result, err = printCapabilities(fieldValue, 10)
if err != nil {
return "", err
}
case typeSuccess:
result, err = printSuccess(fieldValue)
if err != nil {
return "", err
}
case typeA0:
result, err = printA0(fieldValue, r.syscallNum)
if err != nil {
return "", err
}
case typeA1:
result, err = printA1(fieldValue, r.syscallNum, r.a0)
if err != nil {
return "", err
}
case typeA2:
result, err = printA2(fieldValue, r.syscallNum, r.a1)
if err != nil {
return "", err
}
case typeA3:
result, err = printA3(fieldValue, r.syscallNum)
if err != nil {
return "", err
}
case typeSignal:
result, err = printSignals(fieldValue, 10)
if err != nil {
return "", err
}
case typeList:
result, err = printList(fieldValue)
if err != nil {
return "", err
}
case typeTTYData:
// TODO: add printTTYData (see interpret.c (auparse) for ideas)
// result, err = printTTYData(fieldValue)
// if err != nil {
// return "", err
// }
case typeSession:
result, err = printSession(fieldValue)
if err != nil {
return "", err
}
case typeCapBitmap:
// TODO: add printCapBitMap (see interpret.c (auparse) for ideas)
// result, err = printCapBitMap(fieldValue)
// if err != nil {
// return "", err
// }
case typeNFProto:
result, err = printNFProto(fieldValue)
if err != nil {
return "", err
}
case typeICMP:
result, err = printICMP(fieldValue)
if err != nil {
return "", err
}
case typeProtocol:
// getprotobynumber
// result, err = printProtocol(fieldValue)
// if err != nil {
// return "", err
// }
case typeAddr:
result, err = printAddr(fieldValue)
if err != nil {
return "", err
}
case typePersonality:
result, err = printPersonality(fieldValue)
if err != nil {
return "", err
}
case typeOFlag:
result, err = printOpenFlags(fieldValue)
if err != nil {
return "", err
}
case typeSeccomp:
result, err = printSeccompCode(fieldValue)
if err != nil {
return "", err
}
case typeMmap:
result, err = printMmap(fieldValue)
if err != nil {
return "", err
}
case typeProctile:
// Printing proctitle is same as printing escaped
result, err = printEscaped(fieldValue)
if err != nil {
return "", err
}
case typeMacLabel:
fallthrough
case typeUnclassified:
fallthrough
default:
result = fieldValue
}
return result, nil
}
func printUID(fieldValue string) (string, error) {
name, err := user.LookupId(fieldValue)
if err != nil {
return fmt.Sprintf("unknown(%v)", fieldValue), nil
}
return name.Username, nil
}
func printGID(fieldValue string) (string, error) {
return fieldValue, nil
}
func printSyscall(fieldValue string) (string, error) {
name, err := syscallToName(fieldValue)
if err != nil {
return "", err
}
return name, nil
}
func printArch() (string, error) {
return runtime.GOARCH, nil
}
func printExit(fieldValue string) (string, error) {
ival, err := strconv.ParseInt(fieldValue, 10, 64)
if err != nil {
return "", err
}
// c version of this method tries to retrieve string description of the error code
// ignoring the same approach as the codes can vary
if ival == 0 {
return "success", nil
}
return fieldValue, nil
}
func printPerm(fieldValue string) (string, error) {
ival, err := strconv.ParseInt(fieldValue, 10, 64)
if err != nil {
return "", err
}
var perm string
if ival == 0 {
ival = 0x0F
}
if ival&AUDIT_PERM_READ > 0 {
perm += "read"
}
if ival&AUDIT_PERM_WRITE > 0 {
if len(perm) > 0 {
perm += ",write"
} else {
perm += "write"
}
}
if ival&AUDIT_PERM_EXEC > 0 {
if len(perm) > 0 {
perm += ",exec"
} else {
perm += "exec"
}
}
if ival&AUDIT_PERM_ATTR > 0 {
if len(perm) > 0 {
perm += ",attr"
} else {
perm += "attr"
}
}
return perm, nil
}
func printMode(fieldValue string, base int) (string, error) {
ival, err := strconv.ParseInt(fieldValue, base, 64)
if err != nil {
return "", err
}
var name string
firstIFMTbit := syscall.S_IFMT & ^(syscall.S_IFMT - 1)
if syscall.S_IFMT&ival == syscall.S_IFSOCK {
name = "socket"
} else if syscall.S_IFMT&ival == syscall.S_IFBLK {
name = "block"
} else if syscall.S_IFMT&ival == syscall.S_IFREG {
name = "file"
} else if syscall.S_IFMT&ival == syscall.S_IFDIR {
name = "dir"
} else if syscall.S_IFMT&ival == syscall.S_IFCHR {
name = "character"
} else if syscall.S_IFMT&ival == syscall.S_IFIFO {
name = "fifo"
} else if syscall.S_IFMT&ival == syscall.S_IFLNK {
name = "link"
} else {
name += fmt.Sprintf("%03o", (int(ival)&syscall.S_IFMT)/firstIFMTbit)
}
// check on special bits
if ival&syscall.S_ISUID > 0 {
name += ",suid"
}
if ival&syscall.S_ISGID > 0 {
name += ",sgid"
}
if ival&syscall.S_ISVTX > 0 {
name += ",sticky"
}
// the read, write, execute flags in octal
name += fmt.Sprintf("%03o", ((syscall.S_IRWXU | syscall.S_IRWXG | syscall.S_IRWXO) & int(ival)))
return name, nil
}
func printModeShort(fieldValue string, base int) (string, error) {
ival, err := strconv.ParseInt(fieldValue, base, 64)
if err != nil {
return "", err
}
return printModeShortInt(ival)
}
func printModeShortInt(ival int64) (string, error) {
var name string
// check on special bits
if ival&syscall.S_ISUID > 0 {
name += "suid"
}
if ival&syscall.S_ISGID > 0 {
if len(name) > 0 {
name += ","
}
name += "sgid"
}
if ival&syscall.S_ISVTX > 0 {
if len(name) > 0 {
name += ","
}
name += "sticky"
}
name += fmt.Sprintf("0%03o", ((syscall.S_IRWXU | syscall.S_IRWXG | syscall.S_IRWXO) & int(ival)))
return name, nil
}
func printSockAddr(fieldValue string) (string, error) {
// representations of c struct to unpack bytestream into
type sockaddr struct {
SaFamily uint16 `struc:"uint16,little"` // address family, AF_xxx
SaData [14]byte `struc:"[14]byte,little"` // 14 bytes of protocol address
}
type sockaddrUn struct {
SunFamily uint16 `struc:"uint16,little"` /* AF_UNIX */
SunPath [108]byte `struc:"[108]byte,little"` /* pathname */
}
type sockaddrNl struct {
SunFamily uint16 `struc:"uint16,little"` /* AF_NETLINK */
NlPad uint16 `struc:"uint16,little"` /* Zero. */
NlPid int32 `struc:"int32,little"` /* Port ID. */
NlGroups uint32 `struc:"uint32,little"` /* Multicast groups mask. */
}
type sockaddrLl struct {
SllFamily uint16 `struc:"uint16,little"` /* Always AF_PACKET */
SllProtocol uint16 `struc:"uint16,little"` /* Physical-layer protocol */
SllIfindex int32 `struc:"int32,little"` /* Interface number */
SllHatype uint16 `struc:"uint16,little"` /* ARP hardware type */
SllPkttype byte `struc:"byte,little"` /* Packet type */
SllHalen byte `struc:"byte,little"` /* Length of address */
SllAddr [8]byte `struc:"[8]byte,little"` /* Physical-layer address */
}
type sockaddrIn struct {
SinFamily uint16 `struc:"uint16,little"` // e.g. AF_INET, AF_INET6
SinPort uint16 `struc:"uint16,big"` // port in network byte order
InAddr [4]byte `struc:"[4]byte,big"` // address in network byte order
SinZero [8]byte `struc:"[8]byte,little"`
}
type sockaddrIn6 struct {
Sin6Family uint16 `struc:"uint16,little"` // address family, AF_INET6
Sin6Port uint16 `struc:"uint16,big"` // port in network byte order
Sin6Flowinfo uint32 `struc:"uint32,little"` // IPv6 flow information
Sin6Addr [16]byte `struc:"[16]byte,big"` // IPv6 address
Sin6ScopeID uint32 `struc:"uint32,little"` // Scope ID
}
var (
name string
s sockaddr
)
bytestr, err := hex.DecodeString(fieldValue)
if err != nil {
return fieldValue, err
}
buf := bytes.NewBuffer(bytestr)
err = struc.Unpack(buf, &s)
if err != nil {
return fieldValue, err
}
family := int(s.SaFamily)
if _, ok := headers.SocketFamLookup[int(family)]; !ok {
return fmt.Sprintf("unknown family (%v)", family), nil
}
errstring := fmt.Errorf("%s (error resolving addr)", headers.SocketFamLookup[family])
switch family {
case syscall.AF_LOCAL:
var p sockaddrUn
nbuf := bytes.NewBuffer(bytestr)
err = struc.Unpack(nbuf, &p)
if err != nil {
return fieldValue, errstring
}
name = fmt.Sprintf("%s %s", headers.SocketFamLookup[family], string(p.SunPath[:]))
return name, nil
case syscall.AF_INET:
var ip4 sockaddrIn
nbuf := bytes.NewBuffer(bytestr)
err = struc.Unpack(nbuf, &ip4)
if err != nil {
return fieldValue, errstring
}
addrBytes := ip4.InAddr[:]
var x net.IP = addrBytes
name = fmt.Sprintf("%s host:%s serv:%d", headers.SocketFamLookup[family], x.String(), ip4.SinPort)
return name, nil
case syscall.AF_INET6:
var ip6 sockaddrIn6
nbuf := bytes.NewBuffer(bytestr)
err = struc.Unpack(nbuf, &ip6)
if err != nil {
return fieldValue, errstring
}
addrBytes := ip6.Sin6Addr[:]
var x net.IP = addrBytes
name = fmt.Sprintf("%s host:%s serv:%d", headers.SocketFamLookup[family], x.String(), ip6.Sin6Port)
return name, nil
case syscall.AF_NETLINK:
var n sockaddrNl
nbuf := bytes.NewBuffer(bytestr)
err = struc.Unpack(nbuf, &n)
if err != nil {
return fieldValue, errstring
}
name = fmt.Sprintf("%s pid:%d", headers.SocketFamLookup[family], n.NlPid)
return name, nil
case syscall.AF_PACKET:
var l sockaddrLl
nbuf := bytes.NewBuffer(bytestr)
err = struc.Unpack(nbuf, &l)
if err != nil {
return fieldValue, errstring
}
// TODO: decide on kind of information to return
// currently only returning the family name
// name = fmt.Sprintf("%s pid:%u", famLookup[family], l.)
return headers.SocketFamLookup[family], nil
}
return headers.SocketFamLookup[family], nil
}
// this is currently just a stub as its only used in RHEL kernels, interpretation
// can be added, see auparse -> interpret.c -> print_flags()
func printFlags(fieldValue string) (string, error) {
return fieldValue, nil
}
func printEscaped(fieldValue string) (string, error) {
if fieldValue[0] == '"' && fieldValue[len(fieldValue)-1] == '"' {
return fieldValue[1 : len(fieldValue)-1], nil
}
if strings.HasPrefix(fieldValue, "00") {
newStr := unescape(fieldValue[2:])
if newStr == "" {
return fieldValue, nil
}
}
newStr := unescape(fieldValue)
if newStr == "" {
return fieldValue, nil
}
return newStr, nil
}
func unescape(fieldvalue string) string {
if strings.HasPrefix(fieldvalue, "(") {
return fieldvalue
}
if len(fieldvalue) < 2 {
return ""
}
var str []byte
// try to chop 2 characters at a time and convert them from hexadecimal to decimal
str, err := hex.DecodeString(fieldvalue)
if err != nil {
return fieldvalue
}
return string(str)
}
func printPromiscuous(fieldValue string) (string, error) {
ival, err := strconv.ParseInt(fieldValue, 10, 64)
if err != nil {
return "", err
}
if ival == 0 {
return "no", nil
}
return "yes", nil
}
func printCapabilities(fieldValue string, base int) (string, error) {
ival, err := strconv.ParseInt(fieldValue, base, 64)
if err != nil {
return "", err
}
cap, ok := headers.CapabLookup[int(ival)]
if ok {
return cap, nil
}
if base == 16 {
return fmt.Sprintf("unknown capability(0x%d)", ival), nil
}
return fmt.Sprintf("unknown capability(%d)", ival), nil
}
func printSuccess(fieldValue string) (string, error) {
ival, err := strconv.ParseInt(fieldValue, 10, 64)
if err != nil {
// if we are unable to parse success values just return them as is,
// behaviour is the same as auparse interpret.c
return fieldValue, nil
}
const (
sUnset = -1
sFailed = iota
sSuccess
)
switch int(ival) {
case sSuccess:
return "yes", nil
case sFailed:
return "no", nil
default:
return "unset", nil
}
}
func printA0(fieldValue string, sysNum string) (string, error) {
name, err := syscallToName(sysNum)
if err != nil {
return "", err
}
if strings.HasPrefix(name, "r") {
if name == "rt_sigaction" {
return printSignals(fieldValue, 16)
} else if name == "renameat" {
return printDirFd(fieldValue)
} else if name == "readlinkat" {
return printDirFd(fieldValue)
}
} else if strings.HasPrefix(name, "c") {
if name == "clone" {
return printCloneFlags(fieldValue)
} else if name == "clock_settime" {
return printClockID(fieldValue)
}
} else if strings.HasPrefix(name, "p") {
if name == "personality" {
return printPersonality(fieldValue)
} else if name == "ptrace" {
return printPtrace(fieldValue)
} else if name == "prctl" {
return printPrctlOpt(fieldValue)
}
} else if strings.HasPrefix(name, "m") {
if name == "mkdirat" {
return printDirFd(fieldValue)
} else if name == "mknodat" {
return printDirFd(fieldValue)
}
} else if strings.HasPrefix(name, "f") {
if name == "fchownat" {
return printDirFd(fieldValue)
} else if name == "futimesat" {
return printDirFd(fieldValue)
} else if name == "fchmodat" {
return printDirFd(fieldValue)
} else if name == "faccessat" {
return printDirFd(fieldValue)
} else if name == "ftimensat" {
return printDirFd(fieldValue)
}
} else if strings.HasPrefix(name, "u") {
if name == "unshare" {
return printCloneFlags(fieldValue)
} else if name == "unlinkat" {
return printDirFd(fieldValue)
} else if name == "utimesat" {
return printDirFd(fieldValue)
} else if name == "etrlimit" {
return printRLimit(fieldValue)
}
} else if strings.HasPrefix(name, "s") {
if name == "setuid" {
return printUID(fieldValue)
} else if name == "setreuid" {
return printUID(fieldValue)
} else if name == "setresuid" {
return printUID(fieldValue)
} else if name == "setfsuid" {
return printUID(fieldValue)
} else if name == "setgid" {
return printGID(fieldValue)
} else if name == "setregid" {
return printGID(fieldValue)
} else if name == "setresgid" {
return printGID(fieldValue)
} else if name == "socket" {
return printSocketDomain(fieldValue)
} else if name == "setfsgid" {
return printGID(fieldValue)
} else if name == "socketcall" {
return printSocketCall(fieldValue, 16)
}
} else if name == "linkat" {
return printDirFd(fieldValue)
} else if name == "newfsstat" {
return printDirFd(fieldValue)
} else if name == "openat" {
return printDirFd(fieldValue)
} else if name == "ipccall" {
return printIpcCall(fieldValue, 16)
}
return fmt.Sprintf("0x%s", fieldValue), nil
}
func printSignals(fieldValue string, base int) (string, error) {
ival, err := strconv.ParseInt(fieldValue, base, 64)
if err != nil {
return "", err
}
if ival < 31 {
return headers.SignalLookup[int(ival)], nil
}
if base == 16 {
return fmt.Sprintf("unknown signal (0x%s)", fieldValue), nil
}
return fmt.Sprintf("unknown signal (%s)", fieldValue), nil
}
func printDirFd(fieldValue string) (string, error) {
if fieldValue == "-100" {
return "AT_FDWD", nil
}
return fmt.Sprintf("0x%s", fieldValue), nil
}
func printCloneFlags(fieldValue string) (string, error) {
ival, err := strconv.ParseInt(fieldValue, 16, 64)
if err != nil {
return "", err
}
var name string
for key, val := range headers.CloneLookUp {
if key&int(ival) > 0 {
if len(name) > 0 {
name += "|"
}
name += val
}
}
var cloneSignal = ival & 0xFF
if cloneSignal > 0 && cloneSignal < 32 {
if len(name) > 0 {
name += "|"
}
name += headers.SignalLookup[int(cloneSignal)]
}
if len(name) == 0 {
return fmt.Sprintf("0x%d", ival), nil
}
return name, nil
}
func printClockID(fieldValue string) (string, error) {
ival, err := strconv.ParseInt(fieldValue, 16, 64)
if err != nil {
return "", err
}
if ival < 7 {
return headers.ClockLookup[int(ival)], nil
}
return fmt.Sprintf("unknown clk_id (0x%s)", fieldValue), nil
}
// TODO: add personality interpretation
// see auparse -> interpret.c -> print_personality() lookup table persontab.h
func printPersonality(fieldValue string) (string, error) {
return fieldValue, nil
}
func printPtrace(fieldValue string) (string, error) {
ival, err := strconv.ParseInt(fieldValue, 16, 64)
if err != nil {
return "", err
}
if _, ok := headers.PtraceLookup[int(ival)]; !ok {
return fmt.Sprintf("unknown ptrace (0x%s)", fieldValue), nil
}
return headers.PtraceLookup[int(ival)], nil
}
func printPrctlOpt(fieldValue string) (string, error) {
ival, err := strconv.ParseInt(fieldValue, 16, 64)
if err != nil {
return "", err
}
if _, ok := headers.PrctlLookup[int(ival)]; !ok {
return fmt.Sprintf("unknown prctl option (0x%s)", fieldValue), nil
}
return headers.PrctlLookup[int(ival)], nil
}
func printSocketDomain(fieldValue string) (string, error) {
ival, err := strconv.ParseInt(fieldValue, 16, 64)
if err != nil {
return "", err
}
if _, ok := headers.SocketFamLookup[int(ival)]; !ok {
return fmt.Sprintf("unknown family (0x%s)", fieldValue), nil
}
return headers.SocketFamLookup[int(ival)], nil
}
func printSocketCall(fieldValue string, base int) (string, error) {
ival, err := strconv.ParseInt(fieldValue, 16, 64)
if err != nil {
return "", err
}
if _, ok := headers.SockLookup[int(ival)]; !ok {
return fmt.Sprintf("unknown socketcall (0x%s)", fieldValue), nil
}
return headers.SockLookup[int(ival)], nil
}
func printRLimit(fieldValue string) (string, error) {
ival, err := strconv.ParseInt(fieldValue, 16, 64)
if err != nil {
return "", err
}
if _, ok := headers.RlimitLookup[int(ival)]; !ok {
return fmt.Sprintf("unknown rlimit (0x%s)", fieldValue), nil
}
return headers.RlimitLookup[int(ival)], nil
}
func printIpcCall(fieldValue string, base int) (string, error) {
ival, err := strconv.ParseInt(fieldValue, 16, 64)
if err != nil {
return "", err
}
if _, ok := headers.IpccallLookup[int(ival)]; !ok {
return fmt.Sprintf("unknown ipccall (%s)", fieldValue), nil
}
return headers.IpccallLookup[int(ival)], nil
}
func printA1(fieldValue, sysNum string, a0 int) (string, error) {
name, err := syscallToName(sysNum)
if err != nil {
return "", err
}
if strings.HasPrefix(name, "f") {
if name == "fchmod" {
return printModeShort(fieldValue, 16)
} else if name == "fcntl" {
return printFcntlCmd(fieldValue)
}
}
if strings.HasPrefix(name, "c") {
if name == "chmod" {
return printModeShort(fieldValue, 16)
} else if k := strings.Index(name, "chown"); k != -1 {
return printUID(fieldValue)
} else if name == "creat" {
return printModeShort(fieldValue, 16)
}
}
if name[1:] == "etsocketopt" {
return printSockOptLevel(fieldValue)
} else if strings.HasPrefix(name, "s") {
if name == "setreuid" {
return printUID(fieldValue)
} else if name == "setresuid" {
return printUID(fieldValue)
} else if name == "setregid" {
return printGID(fieldValue)
} else if name == "setresgid" {
return printGID(fieldValue)
} else if name == "socket" {
return printSocketType(fieldValue)
} else if name == "setns" {
return printCloneFlags(fieldValue)
} else if name == "sched_setscheduler" {
return printSched(fieldValue)
}
} else if strings.HasPrefix(name, "m") {
if name == "mkdir" {
return printModeShort(fieldValue, 16)
} else if name == "mknod" {
return printMode(fieldValue, 16)
} else if name == "mq_open" {
return printOpenFlags(fieldValue)
}
} else if name == "open" {
return printOpenFlags(fieldValue)
} else if name == "access" {
return printAccess(fieldValue)
} else if name == "epoll_ctl" {
return printEpollCtl(fieldValue)
} else if name == "kill" {
return printSignals(fieldValue, 16)
} else if name == "prctl" {
if a0 == syscall.PR_CAPBSET_READ || a0 == syscall.PR_CAPBSET_DROP {
return printCapabilities(fieldValue, 16)
} else if a0 == syscall.PR_SET_PDEATHSIG {
return printSignals(fieldValue, 16)
}
} else if name == "tkill" {
return printSignals(fieldValue, 16)
} else if name == "umount2" {
return printUmount(fieldValue)
} else if name == "ioctl" {
return printIoctlReq(fieldValue)
}
return fmt.Sprintf("0x%s", fieldValue), nil
}
func printFcntlCmd(fieldValue string) (string, error) {
ival, err := strconv.ParseInt(fieldValue, 16, 64)
if err != nil {
return "", err
}
if _, ok := headers.FcntlLookup[int(ival)]; !ok {
return fmt.Sprintf("unknown fcntl command(%d)", ival), nil
}
return headers.FcntlLookup[int(ival)], nil
}
func printSocketType(fieldValue string) (string, error) {
ival, err := strconv.ParseInt(fieldValue, 16, 64)
if err != nil {
return "", err
}
if _, ok := headers.SockTypeLookup[int(ival)]; !ok {
return fmt.Sprintf("unknown socket type(%d)", ival), nil
}
return headers.SockTypeLookup[int(ival)], nil
}
func printSched(fieldValue string) (string, error) {
const schedResetOnFork int64 = 0x40000000
ival, err := strconv.ParseInt(fieldValue, 16, 64)
if err != nil {
return "", err
}
if _, ok := headers.SchedLookup[int(ival)&0x0F]; !ok {
return fmt.Sprintf("unknown scheduler policy (0x%s)", fieldValue), nil
}
if ival&schedResetOnFork > 0 {
return headers.SchedLookup[int(ival)] + "|SCHED_RESET_ON_FORK", nil
}
return headers.SchedLookup[int(ival)], nil
}
// TODO: add interpretation
// see auparse -> interpret.c -> print_open_flags() for ideas
// useful for debugging rather than forensics actual policy is to filter either on open or write or both
// and emit msg that this happened so if its opened in r,rw, etc. All endup looking the same i.e READ or WRITE
// auparse specific table is open-flagtab.h.
func printOpenFlags(fieldValue string) (string, error) {
// look at table of values from /usr/include/asm-generic/fcntl.h
return fieldValue, nil
}
// policy is to only log success or denial but not read the actual value
// ie make a rule on the arguments but dont read it and just trust that right rule is reported
// auparse specific table is accesstab.h
func printAccess(fieldValue string) (string, error) {
ival, err := strconv.ParseInt(fieldValue, 16, 64)
if err != nil {
return "", err
}
if ival&0x0F == 0 {
return "F_OK", nil
}
var name string
for key, val := range headers.AccessLookUp {
if key&int(ival) > 0 {
if len(name) > 0 {
name += "|"
}
name += val
}
}
if len(name) == 0 {
return fmt.Sprintf("0x%s", fieldValue), nil
}