forked from jaeles-project/jaeles
/
middleware.go
208 lines (184 loc) · 5.58 KB
/
middleware.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
package core
import (
"fmt"
"net"
"net/url"
"os"
"os/exec"
"path"
"strconv"
"strings"
"github.com/hktalent/jaeles/utils"
"github.com/thoas/go-funk"
"github.com/robertkrimen/otto"
)
// @NOTE: Middleware allow execute command on your machine
// So make sure you read the signature before you run it
// Conclude is main function for detections
func (r *Record) MiddleWare() {
//record := *r
vm := otto.New()
var middlewareOutput string
vm.Set("Host2IP", func(call otto.FunctionCall) otto.Value {
var realHeaders []map[string]string
for _, head := range r.Request.Headers {
containHost := funk.Contains(head, "Host")
if containHost == false {
realHeaders = append(realHeaders, head)
}
}
HostHeader := Host2IP(r.Request.URL)
if !funk.IsEmpty(HostHeader) {
realHeaders = append(realHeaders, HostHeader)
}
r.Request.Headers = realHeaders
return otto.Value{}
})
vm.Set("InvokeCmd", func(call otto.FunctionCall) otto.Value {
rawCmd := call.Argument(0).String()
result := InvokeCmd(r, rawCmd)
middlewareOutput += result
utils.DebugF(result)
return otto.Value{}
})
vm.Set("TurboIntruder", func(call otto.FunctionCall) otto.Value {
if r.Request.Raw != "" {
result := TurboIntruder(r)
utils.DebugF(result)
}
return otto.Value{}
})
for _, middleScript := range r.Request.Middlewares {
utils.DebugF("[MiddleWare]: %s", middleScript)
vm.Run(middleScript)
}
r.Request.MiddlewareOutput = middlewareOutput
}
//
//// MiddleWare is main function for middleware
//func MiddleWare(rec *libs.Record, options libs.Options) {
// // func MiddleWare(req *libs.Request) {
// vm := otto.New()
// var middlewareOutput string
//
// vm.Set("Host2IP", func(call otto.FunctionCall) otto.Value {
// var realHeaders []map[string]string
// for _, head := range rec.Request.Headers {
// containHost := funk.Contains(head, "Host")
// if containHost == false {
// realHeaders = append(realHeaders, head)
// }
// }
// HostHeader := Host2IP(rec.Request.URL)
// if !funk.IsEmpty(HostHeader) {
// realHeaders = append(realHeaders, HostHeader)
// }
// rec.Request.Headers = realHeaders
// return otto.Value{}
// })
//
// vm.Set("InvokeCmd", func(call otto.FunctionCall) otto.Value {
// rawCmd := call.Argument(0).String()
// result := InvokeCmd(rec, rawCmd)
// middlewareOutput += result
// utils.DebugF(result)
// return otto.Value{}
// })
//
// vm.Set("TurboIntruder", func(call otto.FunctionCall) otto.Value {
// if rec.Request.Raw != "" {
// result := TurboIntruder(rec)
// utils.DebugF(result)
// }
// return otto.Value{}
// })
//
// for _, middleString := range rec.Request.Middlewares {
// utils.DebugF(middleString)
// vm.Run(middleString)
// }
//
// if middlewareOutput != "" {
// rec.Request.MiddlewareOutput = middlewareOutput
// }
//}
// Host2IP replace Host header with IP address
func Host2IP(rawURL string) map[string]string {
// HostHeader =
u, err := url.Parse(rawURL)
if err != nil {
return map[string]string{}
}
hostname := u.Hostname()
resolved, err := net.LookupHost(hostname)
if err != nil {
return map[string]string{}
}
ip := resolved[0]
if u.Port() == "443" || u.Port() == "80" || u.Port() == "" {
hostname = ip
} else {
hostname = ip + ":" + u.Port()
}
return map[string]string{"Host": hostname}
}
// InvokeCmd execute external command
func InvokeCmd(rec *Record, rawCmd string) string {
target := ParseTarget(rec.Request.URL)
realCommand := Encoder(rec.Request.Encoding, ResolveVariable(rawCmd, target))
utils.DebugF("Execute Command: %v", realCommand)
command := []string{
"bash",
"-c",
realCommand,
}
out, _ := exec.Command(command[0], command[1:]...).CombinedOutput()
rec.Request.MiddlewareOutput = string(out)
return string(out)
}
// TurboIntruder execute Turbo Intruder CLI
func TurboIntruder(rec *Record) string {
req := rec.Request
turboPath := ResolveVariable("{{.homePath}}/plugins/turbo-intruder/turbo-intruder-all.jar", req.Target)
scriptPath := ResolveVariable("{{.homePath}}/plugins/turbo-intruder/basic.py", req.Target)
// create a folder in case it didn't exist
logReqPath := ResolveVariable("{{.homePath}}/log/req/", req.Target)
url := ResolveVariable("{{.URL}}", req.Target)
rec.Request.URL = url
if _, err := os.Stat(logReqPath); os.IsNotExist(err) {
os.MkdirAll(logReqPath, 0750)
}
// write request to a file
rawReq := ResolveVariable(req.Raw, req.Target)
reqPath := path.Join(logReqPath, utils.GenHash(rawReq))
utils.WriteToFile(reqPath, rawReq)
// call the command and parse some info
turboCmd := fmt.Sprintf(`java -jar %v %v %v %v foo`, turboPath, scriptPath, reqPath, url)
command := []string{
"bash",
"-c",
turboCmd,
}
out, _ := exec.Command(command[0], command[1:]...).CombinedOutput()
// parse output
rawOutput := string(out)
if strings.Contains(rawOutput, "=-+-================") {
// split the prefix
resp := strings.Split(rawOutput, "=-+-================")[1]
result := strings.Split(resp, "------------------+=")
// [Info] 403 11585 0.272
info := result[0]
statusCode, _ := strconv.Atoi(strings.Split(info, " ")[1])
rec.Response.StatusCode = statusCode
length, _ := strconv.Atoi(strings.Split(info, " ")[2])
rec.Response.Length = length
resTime, _ := strconv.ParseFloat(strings.TrimSpace(strings.Split(info, " ")[2]), 64)
rec.Response.ResponseTime = resTime
rec.Request.Beautify = result[1]
rec.Response.Beautify = result[2]
verbose := fmt.Sprintf("[TurboIntruder] %v %v %v %v", rec.Request.URL, reqPath, rec.Response.StatusCode, rec.Response.ResponseTime)
return verbose
}
verbose := fmt.Sprintf("[TurboIntruder] Error sending request from: %v", reqPath)
return verbose
}