-
Notifications
You must be signed in to change notification settings - Fork 0
/
gocrypt.go
266 lines (220 loc) · 6.49 KB
/
gocrypt.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
package gocrypt
import (
"bufio"
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
"github.com/Masterminds/sprig"
"github.com/autom8ter/gocrypt/certificates"
"github.com/autom8ter/gocrypt/cron"
"github.com/autom8ter/gocrypt/dos"
"github.com/autom8ter/gocrypt/encrypt"
"github.com/autom8ter/gocrypt/keylogger"
"github.com/autom8ter/gocrypt/keys"
"github.com/autom8ter/gocrypt/load"
"github.com/autom8ter/gocrypt/passwords"
"github.com/autom8ter/gocrypt/utils"
"github.com/howeyc/gopass"
"github.com/spf13/afero"
"github.com/spf13/viper"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
)
type RemoteProvider int
const (
Consul RemoteProvider = iota
Etcd
)
func (r RemoteProvider) String() string {
if r == Etcd {
return "etcd"
}
return "consul"
}
type GoCrypt struct {
loader *load.Client
cache *viper.Viper
fsos *afero.Afero
cro *cron.Cron
}
func NewGoCrypt() *GoCrypt {
t := &afero.Afero{
Fs: afero.NewOsFs(),
}
c := viper.New()
c.SetFs(t)
return &GoCrypt{
loader: load.New(http.DefaultClient),
cache: c,
fsos: t,
cro: cron.New(),
}
}
func (g *GoCrypt) Cache() *viper.Viper {
return g.cache
}
func (g *GoCrypt) Load() *load.Client {
return g.loader
}
func (g *GoCrypt) FS() *afero.Afero {
return g.fsos
}
func (g *GoCrypt) Cron() *cron.Cron {
return g.cro
}
// PrettyJson encodes an item into a pretty (indented) JSON string
func (g *GoCrypt) PrettyJson(v interface{}) string {
output, _ := json.MarshalIndent(v, "", " ")
return string(output)
}
func (g *GoCrypt) Sha256sum(input string) string {
return utils.Sha256sum(input)
}
func (g *GoCrypt) Sha1sum(input string) string {
return utils.Sha1sum(input)
}
func (g *GoCrypt) Adler32sum(input string) string {
return utils.Adler32sum(input)
}
func (g *GoCrypt) Uuidv4() string {
return utils.Uuidv4()
}
func (g *GoCrypt) GeneratePrivateKey(typ keys.KeyType) string {
return typ.Generate()
}
func (g *GoCrypt) BuildCustomCertificate(b64cert string, b64key string) (*certificates.Certificate, error) {
return certificates.BuildCustomCertificate(b64cert, b64key)
}
func (g *GoCrypt) GenerateCertificateAuthority(cn string, daysValid int) (*certificates.Certificate, error) {
return certificates.GenerateCertificateAuthority(cn, daysValid)
}
func (g *GoCrypt) GenerateSelfSignedCertificate(cn string, ips []interface{}, alternateDNS []interface{}, daysValid int) (certificates.Certificate, error) {
return certificates.GenerateSelfSignedCertificate(cn, ips, alternateDNS, daysValid)
}
func (g *GoCrypt) GenerateSignedCertificate(cn string, ips []interface{}, alternateDNS []interface{}, daysValid int, ca certificates.Certificate) (certificates.Certificate, error) {
return certificates.GenerateSignedCertificate(cn, ips, alternateDNS, daysValid, ca)
}
// EncryptFiles Walks documments in a path and encript or decrypts them.
func (g *GoCrypt) EncryptFiles(path string, myKey string, perm os.FileMode, skip ...string) error {
return filepath.Walk(path, encrypt.EncryptPath(myKey, perm, skip...))
}
// DecryptFiles Walks documments in a path and encript or decrypts them.
func (g *GoCrypt) DecryptFiles(path string, myKey string, perm os.FileMode, skip ...string) error {
return filepath.Walk(path, encrypt.DecryptPath(myKey, perm, skip...))
}
func (g *GoCrypt) Prompt(r io.Reader, question string) string {
reader := bufio.NewReader(r)
fmt.Print(question)
text, _ := reader.ReadString('\n')
text = strings.TrimSpace(text)
text = strings.TrimRight(text, "`")
text = strings.TrimLeft(text, "`")
if strings.Contains(text, "?") {
newtext := strings.Split(text, "?")
text = newtext[0]
}
return text
}
func (g *GoCrypt) Render(t string, data interface{}) (string, error) {
if strings.Contains(t, "{{") {
tmpl, err := template.New("").Funcs(sprig.GenericFuncMap()).Parse(t)
if err != nil {
return t, err
}
buf := bytes.NewBuffer(nil)
if err := tmpl.Execute(buf, data); err != nil {
if err != nil {
return t, err
}
}
return buf.String(), nil
}
return t, nil
}
func (g *GoCrypt) StartDDOS(d *dos.DDOS) {
d.Start()
}
func (g *GoCrypt) StopDDOS(d *dos.DDOS) {
d.Stop()
}
func (g *GoCrypt) DerivePassword(counter uint32, master_seed, site string, user, password string) string {
return passwords.DerivePassword(counter, passwords.Long, master_seed, password, user, site)
}
// PromptPassword prompts user for password input.
func (g *GoCrypt) PromptPassword(prompt string) string {
fmt.Printf(prompt)
b, err := gopass.GetPasswd()
if err != nil {
fmt.Println(err.Error())
return ""
}
return string(b)
}
func (g *GoCrypt) GetEnv() []string {
return os.Environ()
}
func (g *GoCrypt) Shell(cmd string) string {
e := exec.Command("/bin/sh", "-c", cmd)
res, _ := e.Output()
return strings.TrimSpace(string(res))
}
func (g *GoCrypt) Bash(cmd string) string {
e := exec.Command("/bin/bash", "-c", cmd)
res, _ := e.Output()
return strings.TrimSpace(string(res))
}
func (g *GoCrypt) Python3(cmd string) string {
e := exec.Command("python3", "-c", cmd)
res, _ := e.Output()
return strings.TrimSpace(string(res))
}
func (g *GoCrypt) Base64Encode(str string) string {
return utils.Base64Encode(str)
}
func (g *GoCrypt) Base64Decode(str string) string {
return utils.Base64Decode(str)
}
func (g *GoCrypt) Base64EncodeRaw(str []byte) string {
return utils.Base64EncodeRaw(str)
}
func (g *GoCrypt) Base64DecodeRaw(str string) []byte {
return utils.Base64DecodeRaw(str)
}
func (g *GoCrypt) RegExFind(exp string, targ string) string {
return utils.RegexFind(exp, targ)
}
func (g *GoCrypt) RegExReplace(exp string, replace string, with string) string {
return utils.RegexReplaceAll(exp, replace, with)
}
func (g *GoCrypt) RegExFindAll(exp string, find string, num int) []string {
return utils.RegexFindAll(exp, find, num)
}
func (g *GoCrypt) RegExSplit(exp string, find string, num int) []string {
return utils.RegexSplit(exp, find, num)
}
func (g *GoCrypt) RegExMatch(exp string, match string) bool {
return utils.RegexMatch(exp, match)
}
func (g *GoCrypt) RemoveAllFiles(path string) error {
return os.RemoveAll(path)
}
func (g *GoCrypt) Walk(path string, walkFunc filepath.WalkFunc) error {
return filepath.Walk(path, walkFunc)
}
func (g *GoCrypt) KeyLog(output afero.File) {
keylogger.LogKeys(output)
}
func (g *GoCrypt) RandomToken(length int) []byte {
b := make([]byte, length)
rand.Read(b)
return b
}
func (g *GoCrypt) AddRemoteCache(provider RemoteProvider, endpoint, path, secretkeyring string) error {
return g.cache.AddSecureRemoteProvider(provider.String(), endpoint, path, secretkeyring)
}