/
grpc_brute.go
168 lines (145 loc) · 4.2 KB
/
grpc_brute.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
package yakgrpc
import (
"context"
_ "embed"
"fmt"
"os"
"strings"
"github.com/yaklang/yaklang/common/utils"
"github.com/yaklang/yaklang/common/utils/bruteutils"
"github.com/yaklang/yaklang/common/yakgrpc/ypb"
)
//go:embed grpc_brute.yak
var startBruteScript string
func (s *Server) StartBrute(params *ypb.StartBruteParams, stream ypb.Yak_StartBruteServer) error {
execParams := make([]*ypb.KVPair, 0)
// reqParams := &ypb.ExecRequest{Script: startBruteScript}
types := utils.PrettifyListFromStringSplited(params.GetType(), ",")
for _, t := range types {
h, err := bruteutils.GetBruteFuncByType(t)
if err != nil || h == nil {
return utils.Errorf("brute type: %v is not available", t)
}
}
execParams = append(execParams, &ypb.KVPair{
Key: "types",
Value: params.GetType(),
})
targetFile, err := utils.DumpHostFileWithTextAndFiles(params.Targets, "\n", params.TargetFile)
if err != nil {
return err
}
defer os.RemoveAll(targetFile)
execParams = append(execParams, &ypb.KVPair{
Key: "target-file",
Value: targetFile,
})
// 解析用户名
userListFile, err := utils.DumpFileWithTextAndFiles(
strings.Join(params.Usernames, "\n"), "\n", params.UsernameFile,
)
if err != nil {
return err
}
defer os.RemoveAll(userListFile)
execParams = append(execParams, &ypb.KVPair{
Key: "user-list-file",
Value: userListFile,
})
// 是否使用默认字典?
if params.GetReplaceDefaultPasswordDict() {
execParams = append(execParams, &ypb.KVPair{
Key: "replace-default-password-dict",
Value: "",
})
}
if params.GetReplaceDefaultUsernameDict() {
execParams = append(execParams, &ypb.KVPair{
Key: "replace-default-username-dict",
Value: "",
})
}
// 解析密码
passListFile, err := utils.DumpFileWithTextAndFiles(
strings.Join(params.Passwords, "\n"), "\n", params.PasswordFile,
)
if err != nil {
return err
}
defer os.RemoveAll(passListFile)
execParams = append(execParams, &ypb.KVPair{
Key: "pass-list-file",
Value: passListFile,
})
// ok to stop
if params.GetOkToStop() {
execParams = append(execParams, &ypb.KVPair{Key: "ok-to-stop", Value: ""})
}
if params.GetConcurrent() > 0 {
execParams = append(execParams, &ypb.KVPair{Key: "concurrent", Value: fmt.Sprint(params.GetConcurrent())})
}
if params.GetTargetTaskConcurrent() > 0 {
execParams = append(execParams, &ypb.KVPair{Key: "task-concurrent", Value: fmt.Sprint(params.GetTargetTaskConcurrent())})
}
if params.GetDelayMin() > 0 && params.GetDelayMax() > 0 {
execParams = append(execParams, &ypb.KVPair{Key: "delay-min", Value: fmt.Sprint(params.GetDelayMin())})
execParams = append(execParams, &ypb.KVPair{Key: "delay-max", Value: fmt.Sprint(params.GetDelayMax())})
}
return s.debugScript(
"", "yak", startBruteScript, stream, execParams, nil,
)
}
func (s *Server) GetAvailableBruteTypes(ctx context.Context, req *ypb.Empty) (*ypb.GetAvailableBruteTypesResponse, error) {
return &ypb.GetAvailableBruteTypesResponse{
Types: bruteutils.GetBuildinAvailableBruteType(),
TypesWithChild: BuildInBruteTypeTree,
}, nil
}
var BuildInBruteTypeTree = GetBuildinAvailableBruteTypeTree(bruteutils.AuthFunctionMap)
func GetBuildinAvailableBruteTypeTree(typeMap []struct {
Name string
Data string
}) []*ypb.Tree {
res := make(map[string]*ypb.Tree)
ret := make([]*ypb.Tree, 0)
newTopLevel := func(name, data string) *ypb.Tree {
// create top level
item := &ypb.Tree{
Name: name,
Data: data,
}
// append to result, just return top-level tree
ret = append(ret, item)
// mark in map, for second level
res[name] = item
return item
}
newSecondLevel := func(group, name, data string) {
// get parent node from top level
tree, ok := res[group]
if !ok {
// create parent node
tree = newTopLevel(group, "")
}
// create child node, append to parent node's children
tree.Children = append(tree.Children, &ypb.Tree{
Name: name,
Data: data,
})
}
for _, item := range typeMap {
data := item.Data
name := item.Name
names := strings.Split(name, "/")
if len(names) == 1 {
// top level
newTopLevel(name, data)
continue
}
// second level
groupName := names[0]
itemName := names[1]
newSecondLevel(groupName, itemName, data)
}
return ret
}