/
redirect.go
268 lines (258 loc) · 6.22 KB
/
redirect.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
package finger
import (
"fmt"
"github.com/projectdiscovery/gologger"
"github.com/robertkrimen/otto"
"golang.org/x/net/html"
"net/url"
"strings"
)
func extractUri(n *html.Node) string {
// 处理 <meta http-equiv="Refresh" content="0;url=/yyoa/index.jsp"> 标签
for _, attr := range n.Attr {
if attr.Key == "content" {
parts := strings.Split(attr.Val, ";")
for _, part := range parts {
if strings.Contains(strings.ToLower(part), "url=") {
redirectURL := strings.TrimSpace(strings.SplitN(part, "=", 2)[1])
return redirectURL
}
}
}
}
return ""
}
func findRefresh(n *html.Node) string {
var uri string
if n.Type == html.ElementNode && n.Data == "meta" {
for _, attr := range n.Attr {
if strings.ToLower(attr.Key) == "http-equiv" && strings.ToLower(attr.Val) == "refresh" {
uri = extractUri(n)
if uri != "" {
return uri
}
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
uri = findRefresh(c)
if uri != "" {
return uri
}
}
return ""
}
func getExecRedirect(uri string, jsCodes []string, onload string) string {
vm := otto.New()
parsed, _ := url.Parse(uri)
initWindowsCode := fmt.Sprintf(`
var location = {href:"%s",hostname:"%s","protocol":"%s"};
var window = {
location: location,
open: function(url, target) {
window.location.href =url;
}
};
var top = {
window: window,
location: location,
document: {
location: window.location
}
}
var document = {
location: window.location,
};
window.top = top;
self = window;
`, uri, parsed.Hostname(), parsed.Scheme)
_, err := vm.Run(initWindowsCode)
if err != nil {
gologger.Debug().Msgf("Error getting result:%v", err)
}
var consoleLogs []string
_ = vm.Set("console", map[string]interface{}{
"log": func(call otto.FunctionCall) otto.Value {
for _, arg := range call.ArgumentList {
value, _ := arg.Export()
consoleLogs = append(consoleLogs, fmt.Sprint(value))
}
return otto.Value{}
},
})
if err != nil {
fmt.Println("JavaScript execution error:", err)
return ""
}
for _, code := range jsCodes {
_, err = vm.Run(code)
if err != nil {
gologger.Debug().Msgf("Error getting result:%v", err)
return ""
}
}
code := fmt.Sprintf(`
%s
if (window.onload) {
window.onload();
}
if(top.location){
window.location = top.location
}
if(location.href){
window.location.href = location.href;
}
if(top.document.location.href){
window.location.href = top.document.location.href;
}
var finalHref;
if (Object.prototype.toString.call(window.location)=== '[object Object]') {
finalHref = window.location.href;
}else{
finalHref = window.location;
}
`, onload)
_, err = vm.Run(code)
for _, log := range consoleLogs {
fmt.Println(">>|", log)
}
if err != nil {
gologger.Debug().Msgf("Error getting result:%v", err)
return ""
}
// 获取执行后的地址
result, err := vm.Get("finalHref")
if err != nil {
gologger.Debug().Msgf("Error getting result:%v", err)
return ""
}
r2 := result.String()
if r2 == uri {
return ""
}
return result.String()
}
func findAttribute(attrs []html.Attribute, key string) string {
for _, attr := range attrs {
if attr.Key == key {
return attr.Val
}
}
return ""
}
func extractCharset(htmlContent string) string {
reader := strings.NewReader(htmlContent)
doc, err := html.Parse(reader)
if err != nil {
return "UTF-8"
}
var charset string
var traverse func(*html.Node)
traverse = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "meta" {
for _, attr := range n.Attr {
if attr.Key == "http-equiv" && strings.EqualFold(attr.Val, "Content-Type") {
contentAttr := findAttribute(n.Attr, "content")
charsetIndex := strings.Index(contentAttr, "charset=")
if charsetIndex != -1 {
charset = contentAttr[charsetIndex+len("charset="):]
break
}
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
traverse(c)
}
}
traverse(doc)
return strings.ToUpper(strings.TrimSpace(charset))
}
func parseJavaScript(url string, htmlContent string) string {
// 在这里解析JavaScript,提取跳转信息
//<meta http-equiv="Refresh"content="0;url=/yyoa/index.jsp">
doc, err := html.Parse(strings.NewReader(htmlContent))
if err != nil {
return ""
}
var uri string
for c := doc.FirstChild; c != nil; c = c.NextSibling {
uri = findRefresh(c)
if uri != "" {
return uri
}
}
var visitNode func(n *html.Node)
var scripts []string
var onload string
visitNode = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "body" {
for _, attr := range n.Attr {
if attr.Key == "onload" {
onload = attr.Val
}
}
}
// 如果节点是一个script标签并且包含JavaScript代码,则将其添加到切片中
if n.Type == html.ElementNode && n.Data == "script" {
var jsCode string
// 提取script标签内的文本内容
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.TextNode {
jsCode += c.Data
}
}
// 将JavaScript代码添加到切片中
if jsCode != "" {
scripts = append(scripts, jsCode)
}
}
// 递归遍历子节点
for c := n.FirstChild; c != nil; c = c.NextSibling {
visitNode(c)
}
}
visitNode(doc)
if len(scripts) > 2 {
return ""
} else {
// parse onload
if strings.HasPrefix(onload, "javascript:") {
return getExecRedirect(url, scripts, strings.Split(onload, ":")[1])
} else {
return getExecRedirect(url, scripts, "")
}
}
}
func urlJoin(base, path string) string {
if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
return path
}
if strings.HasPrefix(path, "../") {
// todo last path
path = strings.TrimPrefix(path, "..")
}
if base[len(base)-1] != '/' && path[0] != '/' {
base += "/"
}
if base[len(base)-1] == '/' && path[0] == '/' {
path = path[1:]
}
return base + path
}
func joinURL(baseURL, path string) string {
base, err := url.Parse(baseURL)
if err != nil {
return ""
}
var ref = &url.URL{}
if strings.Contains(path, "?") {
ref.Path = strings.Split(path, "?")[0]
ref.RawQuery = strings.Split(path, "?")[1]
} else {
ref.Path = strings.Split(path, "?")[0]
}
// 使用 ResolveReference 方法拼接路径
fullURL := base.ResolveReference(ref)
return fullURL.String()
}