Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ee/agent-smith/cmd/signature-matches.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"encoding/json"
9
"fmt"
10
"os"
11
12
"github.com/gitpod-io/gitpod/agent-smith/pkg/classifier"
13
"github.com/gitpod-io/gitpod/agent-smith/pkg/config"
14
15
"github.com/gitpod-io/gitpod/common-go/log"
16
"github.com/spf13/cobra"
17
)
18
19
// signatureElfdumpCmd represents the signatureElfdump command
20
var signatureMatchesCmd = &cobra.Command{
21
Use: "matches <binary>",
22
Short: "Finds all signatures that match the binary",
23
Args: cobra.MinimumNArgs(1),
24
Run: func(cmd *cobra.Command, args []string) {
25
f, err := os.OpenFile(args[0], os.O_RDONLY, 0644)
26
if err != nil {
27
log.Fatal(err)
28
}
29
defer f.Close()
30
31
sfc := classifier.SignatureReadCache{
32
Reader: f,
33
}
34
35
if cfgFile == "" {
36
log.Info("no config present - reading signature from STDIN")
37
var sig classifier.Signature
38
err := json.NewDecoder(os.Stdin).Decode(&sig)
39
if err != nil {
40
log.Fatal(err)
41
}
42
43
match, err := sig.Matches(&sfc)
44
if err != nil {
45
log.Fatal(err)
46
}
47
48
if !match {
49
fmt.Println("no match")
50
os.Exit(1)
51
}
52
fmt.Println(sig)
53
return
54
}
55
56
cfg, err := config.GetConfig(cfgFile)
57
if err != nil {
58
log.WithError(err).Fatal("cannot get config")
59
}
60
if cfg.Blocklists == nil {
61
log.WithError(err).Fatal("no signatures configured")
62
}
63
64
var res []*classifier.Signature
65
for _, bl := range cfg.Blocklists.Levels() {
66
for _, s := range bl.Signatures {
67
m, err := s.Matches(&sfc)
68
if err != nil {
69
log.WithError(err).WithField("signature", s.Name).Warn("cannot match signature")
70
continue
71
}
72
if !m {
73
log.WithField("signature", s.Name).Debug("no match")
74
continue
75
}
76
res = append(res, s)
77
}
78
}
79
80
if len(res) == 0 {
81
os.Exit(1)
82
}
83
84
for _, s := range res {
85
fmt.Println(s)
86
}
87
},
88
}
89
90
func init() {
91
signatureCmd.AddCommand(signatureMatchesCmd)
92
}
93
94