-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmng.go
786 lines (632 loc) · 18.3 KB
/
dmng.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
// Copyright (c) 2023 Unibg Seclab (https://seclab.unibg.it)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package main
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"os/user"
"path"
"strconv"
"strings"
dmng "dmng/utils"
"github.com/akamensky/argparse"
)
// error check shorthand that panics
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func init() {
user, err := user.Current()
checkErr(err)
PROFILES_FOLDER = "/home/" + user.Username + "/.dmng_profiles"
}
// security profiles folder
var PROFILES_FOLDER string
type Permission = dmng.Permission
type PolicyRow = dmng.PolicyRow
type SecurityProfile = dmng.SecurityProfile
type PolicyIdentifier = dmng.PolicyIdentifier
// parser debug flag
var DEBUG_PARSER bool = false
// active context
var CTX string
// active policy
var POL int
// command
var CMD string
// extended command
var EXTD_CMD string
// permission (mask)
var PERMISSION string
func main() {
/*
CMD LINE PARSER AND ARGUMENTS
*/
parser := argparse.NewParser("dmng", "A command line tool to manage the "+
"requirements associated with a component, binary or script.")
// 1. extended arguments
debug := parser.Flag("", "debug",
&argparse.Options{
Help: "Print additional information to console",
})
serialize := parser.String("", "serialize",
&argparse.Options{
Required: false,
Help: "Serialize the policy to a given filename",
})
fixdepth := parser.String("", "fixdepth",
&argparse.Options{
Required: false,
Help: "Update the `max_depth` attribute of a given policy according" +
" to the content of `Deny`",
})
setctx := parser.String("", "setcontext",
&argparse.Options{
Required: false,
Help: "Set the active policy context of a component. A policy context is active" +
" until it's replaced by another one.",
})
getctx := parser.Flag("", "getcontext",
&argparse.Options{
Required: false,
Help: "Get the active policy context",
})
profile := parser.Flag("", "profile",
&argparse.Options{
Required: false,
Help: "Print to console the profile of a component",
})
// 2. abbreviated arguments
command := parser.String("c", "component",
&argparse.Options{
Required: false,
Help: "Set the component name",
})
add := parser.String("a", "add",
&argparse.Options{
Required: false,
Help: "Add a requirement (the absolute path) to the active policy",
})
remove := parser.String("r", "remove",
&argparse.Options{
Required: false,
Help: "Remove a requirement from the active policy. SQL-like requirement" +
" patterns supported",
})
update := parser.String("u", "update",
&argparse.Options{
Required: false,
Help: "Update the permission associated with a requirement in the active policy." +
" SQL-like requirement patterns supported",
})
permission := parser.String("p", "permission",
&argparse.Options{
Required: false,
Help: "Set the Unix-like permission mask. Valid symbols are `r`, `w`, `x`, `-`." +
" Order matters",
})
deny := parser.Flag("d", "deny",
&argparse.Options{
Help: "Flag to add/remove/update a requirement to/from the deny list. To be used" +
" in conjunction with `--add`, `--remove`, `--update` or `--wipe`",
})
inspect := parser.Flag("i", "inspect",
&argparse.Options{
Required: false,
Help: "Print to console the entries stored by the current policy",
})
trace := parser.Selector(
"t", "trace", []string{"static", "ptrace", "ebpf"},
&argparse.Options{
Required: false,
Help: "Trace program and automatically add its requirements to the active policy. " +
"Three modes are supported: 'static', 'ptrace' and 'ebpf'",
},
)
simulate := parser.Float("s", "simulate",
&argparse.Options{
Required: false,
Help: "Set dynamic tracing. To be used in conjuction with `--trace`. " +
"Requires the simulation time in seconds (e.g., 1, 2, 3.5)",
})
build := parser.Flag("b", "build",
&argparse.Options{
Required: false,
Help: "Build the security profile for the active policy",
})
goal := parser.Int("g", "goal",
&argparse.Options{
Required: false,
Help: "Set the maximum number of entries in the security profile (default 30)." +
" To be used in conjunction with `--goal`",
})
wipe := parser.Flag("w", "wipe",
&argparse.Options{
Required: false,
Help: "Wipe the entries associated with the active policy. If no other option" +
" is provided, wipes the whole DB",
})
example := parser.Flag("e", "example",
&argparse.Options{
Required: false,
Help: "Print some usage examples",
})
/*
READ CMD LINE INPUT
*/
err := parser.Parse(os.Args)
if err != nil {
fmt.Println(parser.Usage(err))
fmt.Printf("[Error] invalid input\n")
failure()
}
/*
UTILITY
*/
if *example {
printUsageInfo()
success()
} else {
createDB()
if *debug {
enableDebugMode()
}
if *serialize != "" {
BuildProfiles(*goal, *serialize)
success()
}
if *fixdepth != "" {
FixPolicy(*fixdepth)
success()
}
if *permission != "" {
PERMISSION = strings.ToLower(*permission)
}
if *command != "" {
// support also space separated shell commands
sntzCmd := preprocessCommand(strings.Trim(*command, " "))
cmdKeywords := strings.Split(sntzCmd, " ")
CMD = cmdKeywords[0]
EXTD_CMD = CMD
if len(cmdKeywords) > 1 {
EXTD_CMD = strings.Join(cmdKeywords, " ")
}
fmt.Printf("[*] Component to trace: `%s`\n", EXTD_CMD)
if *setctx != "" {
CTX = *setctx
updateContext()
success()
}
if *getctx {
getContext()
success()
}
retrieveActiveContext()
retrieveActivePolicy()
if *trace != "" {
switch *trace {
case "static":
traceStatic()
case "ptrace":
if *simulate <= 0 {
panic("[Error] Dynamic tracing requires to specify the tracing " +
"timeframe with the '--simulate' argument")
}
tracePtrace(*simulate)
case "ebpf":
traceEbpf()
}
success()
} else if *wipe {
isDeny := *deny
removeActivePolicy(isDeny)
success()
} else if *build {
BuildSecurityProfile(*goal)
success()
} else if *inspect {
inspectCommand()
success()
} else if *profile {
printProfile()
success()
} else if *add != "" {
// check
if *remove != "" {
fmt.Println("[Error] Cannot use `--add` and `--remove` at the same time")
failure()
}
path := *add
isDeny := *deny
addPath(path, isDeny)
success()
} else if *remove != "" {
path := *remove
isDeny := *deny
removePath(path, isDeny)
success()
} else if *update != "" {
path := *update
updatePath(path)
success()
}
} else if *wipe {
wipeDB()
success()
} else if *getctx {
getAvailableContexts()
success()
}
}
// you shouldn't end up here
printUsageInfo()
failure()
}
/*
UTILITY FUNCTIONS
*/
func success() {
os.Exit(0)
}
func failure() {
os.Exit(1)
}
func getContext() {
c := dmng.GetActiveContext(CMD)
fmt.Printf("[*] COMP: %s, CTX: %s\n", CMD, c)
}
func getAvailableContexts() {
data := *dmng.GetAvailableContexts()
for k, v := range data {
fmt.Printf("[*] COMP %s: , available CTX: |", k)
for _, e := range v {
fmt.Printf(" %s |", e)
}
fmt.Println()
}
}
func printUsageInfo() {
user, err := user.Current()
checkErr(err)
infoPath := "/home/" + user.Username + "/.config/dmng/usage.txt"
info, err := ioutil.ReadFile(infoPath)
checkErr(err)
fmt.Println(string(info))
}
func createDB() {
// create the database if it doesn't exists
dmng.CreateDB()
}
func enableDebugMode() {
// propagate debug mode
dmng.DEBUG_COMMAND = true
dmng.DEBUG_DB = true
dmng.DEBUG_TRUNCATE = true
dmng.DEBUG_SERIALIZER = true
DEBUG_PARSER = true
}
func updateContext() {
// update the profiles-DB cache
dmng.UpdateCacheContext(CMD, CTX)
// set a new active policy in the profiles-DB
dmng.UpdatePolicyContext(CMD, CTX)
// get the active policy context
POL = dmng.GetActivePolicyContext(CMD, CTX)
fmt.Printf("[*] Set CTX: %s, COMP: %s, POL: %d\n", CTX, CMD, POL)
}
func retrieveActiveContext() {
// get the active context from the profiles-DB
CTX = dmng.GetActiveContext(CMD)
}
// Preprocesses multi-keywords commands sanitizing file paths
func preprocessCommand(cmd string) string {
// multi-string check
keywords := strings.Split(cmd, " ")
if len(keywords) == 1 {
return cmd
}
var sntzCmd strings.Builder
// copy program name
fmt.Fprintf(&sntzCmd, "%s ", keywords[0])
cwd, err := os.Getwd()
if err != nil {
fmt.Println("[E] Unable to get the current folder")
failure()
}
basicOps := map[string]bool{
">": true,
">>": true,
"<": true,
"<<": true,
"|": true,
}
// for each token in keywords
for _, tok := range keywords[1:] {
_, err := os.Stat(tok)
// check if tok is file
if _, ok := basicOps[tok]; !ok && err == nil {
// make the path global and sanitize it
if !strings.HasPrefix(tok, "/") {
tok = cwd + "/" + tok
}
tok = path.Clean(tok)
}
fmt.Fprintf(&sntzCmd, "%s ", tok)
// else resolve the file and sanitize it
}
return sntzCmd.String()[:len(sntzCmd.String())-1]
}
func retrieveActivePolicy() {
// get the active policy from the profiles-DB
POL = dmng.GetActivePolicyContext(CMD, CTX)
}
func tracePtrace(simulationTime float64) {
// add to the profiles-DB the chain-of-links to the executable
dmng.GetType(POL, CMD, false)
fmt.Printf("[*] Tracing component with `ptrace`:\t%s\n", CMD)
dmng.Strace(POL, EXTD_CMD, simulationTime)
}
func traceEbpf() {
fmt.Printf("[*] Tracing component with `ebpf`:\t%s\n", CMD)
// when user enters a command
if _, err := strconv.Atoi(CMD); err != nil {
// add to the profiles-DB the chain-of-links to the executable
dmng.GetType(POL, CMD, false)
}
dmng.Ebpf(POL, EXTD_CMD)
}
// Retrieves all the requirements found for the given `command` and
// adds them to the profiles-DB
func traceStatic() {
// get the path and the type associated with the command
command_path, program_type := dmng.GetType(POL, CMD, true)
help_wrapper := "[*] Component wrapper found, try the `--simulate` option"
help_tracepath := "[*] No direct path to the ELF program, try the `--simulate` option"
// print info to console
fmt.Printf("[*] Component path:\t%s\n", command_path)
switch program_type {
case dmng.ELFProgram:
// get the transient libraries of an ELF file
dmng.ELFHandler(POL, CMD, command_path)
case dmng.BourneShellScript:
fmt.Println(help_wrapper)
case dmng.PosixShellScript:
fmt.Println(help_wrapper)
case -1:
// undetermined program type
fmt.Println(help_tracepath)
}
success()
}
func removeActivePolicy(isDeny bool) {
if isDeny {
dmng.WipeDenials(POL)
} else {
dmng.RemoveCommand(POL, CMD, CTX)
}
}
// Builds the security profiles for all the commands in the profiles-DB
// and writes the policy file. `goal` is the maximum number of
// permissioned targets in the permission Trie of each policy. `fname`
// is the filename to store the marshaled policy.
func BuildProfiles(goal int, fname string) {
// for each policy in the profiles-DB
pids := dmng.GetAllAvailablePolicies()
// profiles mapping 1:1 policies
var profiles []SecurityProfile
// build the security profile for each policy
for _, pid := range pids {
// set the vars POL, CMD and CTX
POL = pid.Pol
CMD = pid.Cmd
CTX = pid.Ctx
profiles = append(profiles, BuildSecurityProfile(goal))
fmt.Printf("[*] Policy for COMP: %s and CTX: %s created successfully\n", CMD, CTX)
}
// ensure the dmng_profiles folder is available
createProfilesFolder(fname)
// serialize the policies
dmng.SerializePolicy(pids, profiles, PROFILES_FOLDER, fname)
fmt.Printf("[*] Policy serialized to file %s\n", dmng.POLICY_DIR+"/"+fname)
}
// Updates the content of a serialized policy fixing possible errors in
// the `max_depth` attribute. `pname` is the policy name.
func FixPolicy(pname string) {
dmng.FixDepth(PROFILES_FOLDER, pname)
fmt.Printf("[*] Policy %s updated successfully\n", dmng.POLICY_DIR+"/"+pname)
}
// Builds the security profile of a command, possibly reducing the
// cardinality of requirements to less than or equal to `goal`. Returns
// the entries in the profile created
func BuildSecurityProfile(goal int) SecurityProfile {
if goal < 0 {
fmt.Println("[Error] Invalid number of permissions (goal) found")
failure()
}
if goal == 0 {
// set default value
goal = 50
}
var lof []PolicyRow
// retrieve requirements
lof = dmng.GetCommandRequirements(POL)
// build command Trie
commandTrie := dmng.CreateTrie(CMD, lof)
if DEBUG_PARSER {
fmt.Printf("[D] Initial number of permissioned nodes: %d\n",
commandTrie.CountPermissionedNodes())
}
// print Trie before pruning
var profile_buffer bytes.Buffer
if DEBUG_PARSER {
fmt.Println("[D] Print Trie:")
commandTrie.PrintTrie(&profile_buffer, 0)
fmt.Print(profile_buffer.String())
}
// prune Trie
successful_pruning := commandTrie.PruneTrie(goal)
// print Trie after pruning
if DEBUG_PARSER {
fmt.Println("[D] Print Trie:")
profile_buffer = bytes.Buffer{}
commandTrie.PrintTrie(&profile_buffer, 0)
fmt.Print(profile_buffer.String())
}
if !successful_pruning {
fmt.Printf("[W] Pruning goal not achieved "+
"(still %d nodes)!\n", commandTrie.CountPermissionedNodes())
}
if dmng.DEBUG_TRUNCATE {
// print new profile permissions
fmt.Printf("[*] New %s profile created successfully\n", CMD)
profile_buffer = bytes.Buffer{}
commandTrie.PrintTrieProfile(&profile_buffer)
fmt.Print(profile_buffer.String())
}
// wipe old security profile
dmng.WipeSecurityProfile(POL)
// retrieve the new security profile and save it into the profiles-DB
sp := SecurityProfile{}
commandTrie.GetSecurityProfile(&sp.Entries)
dmng.CreateSecurityProfile(POL, &sp.Entries)
return sp
}
// Creates the policy folder. Removes the old policy serialization
// (`fname`) if present
func createProfilesFolder(fname string) {
// check if the dmng_profiles directory already exists
if _, err := os.Stat(PROFILES_FOLDER); errors.Is(err, os.ErrNotExist) {
err := os.MkdirAll(PROFILES_FOLDER, 0750)
if err != nil {
panic("Unable to create the `" + PROFILES_FOLDER +
"` directory, check your permissions")
}
} else {
err := os.Remove(dmng.POLICY_DIR + "/" + fname)
if err != nil && !errors.Is(err, os.ErrNotExist) {
panic(err)
}
}
// check folder permissions
err := os.Chmod(PROFILES_FOLDER, 0750)
checkErr(err)
}
func printProfile() {
rows := dmng.GetSecurityProfile(POL)
fmt.Printf("[*] Security profile CTX: %s, COMP: %s, POL: %d\n", CTX, CMD, POL)
for i, r := range rows {
fmt.Printf(" %d,\t%s,\t%s\n", i, r.Perm.ToString(), r.Req)
}
}
func inspectCommand() {
// apply mask filter if provided
if PERMISSION != "" {
if !dmng.IsValidMask(PERMISSION) {
fmt.Println("[Error] Invalid permission or permission mask found")
failure()
}
fmt.Printf("[*] Inspect component %s, mask %s\n", CMD, PERMISSION)
fmt.Printf("\n Permissions (CTX: %s, POL: %d):\n", CTX, POL)
dmng.PrintPermissionedRequirements(POL, PERMISSION)
} else {
mask := "___ => ANY"
fmt.Printf("[*] Inspect component %s, mask %s\n", CMD, mask)
fmt.Printf("\n Permissions (CTX: %s, POL: %d):\n", CTX, POL)
// print the requirements
dmng.PrintRequirements(POL)
}
// print the denials
fmt.Printf("\n Denials (CTX: %s, POL: %d):\n", CTX, POL)
dmng.PrintDenials(POL)
}
// Adds a path to the list of requirements of deny
func addPath(path string, isDeny bool) {
// check path
if !dmng.IsValidRequirement(path) {
fmt.Println("[Error] Invalid requirement (file or directory not found)")
failure()
}
if isDeny {
if PERMISSION != "" {
fmt.Println("[Error] Permission not needed with a deny.")
failure()
}
dmng.AddDenial(POL, path)
fmt.Println("[*] Denial for component " + CMD + " added")
} else {
if !dmng.IsValidPerm(PERMISSION) {
fmt.Println("[Error] Invalid permission found")
failure()
}
perm := int(dmng.InitPermission(PERMISSION).ToUnixInt())
dmng.AddRequirement(POL, path, perm, dmng.USER_INPUT)
fmt.Println("[*] Requirement of component " + CMD + " added")
}
}
// Removes a path from the list of requirements or deny
func removePath(path string, isDeny bool) {
if isDeny {
// check
if PERMISSION != "" {
fmt.Println("[Error] Permission not needed with a deny.")
failure()
}
dmng.RemoveDenial(POL, path)
fmt.Println("[*] Denial for component " + CMD + " removed")
} else {
if PERMISSION == "" {
fmt.Println("[Error] `--remove` without permission regex is unsupported, use `--wipe` instead")
failure()
} else {
// check permission regex is valid
if !dmng.IsValidPerm(PERMISSION) {
fmt.Println("[Error] Invalid permission or permission regex found")
failure()
}
perm := int(dmng.InitPermission(PERMISSION).ToUnixInt())
// NB we don't check path to be a valid SQL pattern
dmng.RemovePermissionedRequirements(POL, path, perm)
}
}
}
// Updates a path in the list of requirements
func updatePath(path string) {
// check
if PERMISSION != "" {
if !dmng.IsValidPerm(PERMISSION) {
fmt.Println("[Error] Invalid permission found")
failure()
}
perm := int(dmng.InitPermission(PERMISSION).ToUnixInt())
// NB, we don't check *update to be a valid SQL pattern
dmng.UpdateRequirementPermission(POL, path, perm)
} else {
fmt.Println("[Error] A permission is required if you want to perform an update")
failure()
}
}
func wipeDB() {
// wipe profiles-DB
dmng.WipeProfiles()
}