forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
195 lines (163 loc) · 4.53 KB
/
utils.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
/*
Velociraptor - Hunting Evil
Copyright (C) 2019 Velocidex Innovations.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package crypto
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/binary"
"encoding/hex"
"encoding/pem"
"fmt"
errors "github.com/pkg/errors"
"www.velocidex.com/golang/velociraptor/config"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
)
func ParseRsaPrivateKeyFromPemStr(pem_str []byte) (*rsa.PrivateKey, error) {
for {
block, rest := pem.Decode(pem_str)
if block == nil {
return nil, errors.New("failed to parse PEM block containing the key")
}
if block.Type == "RSA PRIVATE KEY" {
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, errors.WithStack(err)
}
return priv, nil
}
pem_str = rest
}
}
func ParseX509CertFromPemStr(pem_str []byte) (*x509.Certificate, error) {
for {
block, rest := pem.Decode(pem_str)
if block == nil {
return nil, errors.New("Unable to parse PEM")
}
if block.Type == "CERTIFICATE" {
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, errors.WithStack(err)
}
return cert, nil
}
pem_str = rest
}
}
func parseX509CSRFromPemStr(pem_str []byte) (*x509.CertificateRequest, error) {
for {
block, rest := pem.Decode(pem_str)
if block == nil {
return nil, errors.New("Unable to parse PEM")
}
if block.Type == "CERTIFICATE REQUEST" {
csr, err := x509.ParseCertificateRequest(block.Bytes)
if err != nil {
return nil, errors.WithStack(err)
}
return csr, nil
}
pem_str = rest
}
}
/* GRR Client IDs are derived from the public key of the client.
This makes it impossible to impersonate a client unless one has the
client's corresponding private key.
*/
func ClientIDFromPublicKey(public_key *rsa.PublicKey) string {
raw_n := public_key.N.Bytes()
result := make([]byte, 4+1+len(raw_n))
binary.BigEndian.PutUint32(result[0:], uint32(len(raw_n)+1))
copy(result[5:], raw_n)
hashed := sha256.Sum256(result)
dst := make([]byte, hex.EncodedLen(8))
hex.Encode(dst, hashed[:8])
return "C." + string(dst)
}
func GeneratePrivateKey() ([]byte, error) {
reader := rand.Reader
bitSize := 2048
key, err := rsa.GenerateKey(reader, bitSize)
if err != nil {
return nil, errors.WithStack(err)
}
pemdata := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
},
)
return pemdata, nil
}
func PublicKeyToPem(key *rsa.PublicKey) []byte {
return pem.EncodeToMemory(
&pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: x509.MarshalPKCS1PublicKey(key),
},
)
}
func PemToPublicKey(pem_str []byte) (*rsa.PublicKey, error) {
for {
block, rest := pem.Decode(pem_str)
if block == nil {
return nil, errors.New("failed to parse PEM block containing the key")
}
if block.Type == "RSA PUBLIC KEY" {
pub, err := x509.ParsePKCS1PublicKey(block.Bytes)
if err != nil {
return nil, errors.WithStack(err)
}
return pub, nil
}
pem_str = rest
}
}
// Verify the configuration, possibly updating default settings.
func VerifyConfig(config_obj *config_proto.Config) error {
if config_obj.Client == nil || len(config_obj.Client.ServerUrls) == 0 {
return errors.New("No server URLs configured!")
}
if config_obj.Writeback == nil {
config_obj.Writeback = &config_proto.Writeback{}
}
if config_obj.Writeback.PrivateKey == "" {
fmt.Println("Genering new private key....")
pem, err := GeneratePrivateKey()
if err != nil {
return errors.WithStack(err)
}
config_obj.Writeback.PrivateKey = string(pem)
err = config.UpdateWriteback(config_obj)
if err != nil {
return err
}
}
return nil
}
func GetSubjectName(cert *x509.Certificate) string {
if cert.Subject.CommonName != "" {
return cert.Subject.CommonName
}
if len(cert.DNSNames) > 0 {
return cert.DNSNames[0]
}
if len(cert.IPAddresses) > 0 {
return cert.IPAddresses[0].String()
}
return ""
}